wally
wally

Reputation: 11002

std::istream_iterator stops advancing

Why does the following program output

1 2 3 4 4 4

and not

1 2 3 4 5 6

for each of the values provided?

#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <sstream>

int main()
{

    std::vector<int> numbers;
    std::stringstream ss;
    ss << " 1 2";
    std::istream_iterator<int> start{ss},end;
    ss << " 3 4";
    numbers.push_back(*start++);
    numbers.push_back(*start++);
    numbers.push_back(*start++);
    ss << " 5 6";
    numbers.push_back(*start++);
    numbers.push_back(*start++);
    numbers.push_back(*start++);

    std::cout << "numbers read in:\n";
    for (auto number : numbers) {
        std::cout << number << " ";
    }
    std::cout << "\n";
}

Upvotes: 0

Views: 78

Answers (2)

xinaiz
xinaiz

Reputation: 7788

Its not iterator doing as you might have thought. It's ss that is invalidated after iterator progressing. Initialiy stringstream constains 1 2 3 4 and is in valid state. But is invalidated by the third iterator dereference, so next operation ss << " 5 6" fails. To fix this, clear flags of stringstream variable:

//...
ss.clear();
ss << " 5 6";
//...

Output:

numbers read in:
1 2 3 4 5 6

Upvotes: 2

angelvet
angelvet

Reputation: 59

Use stream iterators with some caution. When a valid istream_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator.

And then dereferencing or incrementing it further invokes undefined behavior, in your case you just got a copy of the most recently read object.

Also keep in mind that the first object from the stream is read when the iterator is constructed.

Upvotes: 1

Related Questions