C ++ 11 - removing a stream from a stream

When I have a program that can take up to the ninput line to solve the problem, before starting with the next, I need to throw away all the input lines that are related to the old problem, when the program can solve it before reading all the input data.

Of course, I could just go in cycles until I get to a new problem, but reading and storing all this data can be expensive. Is there no way to simply tell the input stream to ignore the following so-and-many values?

+4
source share
1 answer
void discardLines(std::istream &in, std::size_t count)
{
    if (!in)  
       return;

    for (auto i = count; i != 0; --i)
         in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

istream cin, ifstream istringstream.

Reference std::basic_istream::ignore.

+1

Source: https://habr.com/ru/post/1609968/


All Articles