Input stream iterators are single-pass iterators, meaning once they have been incremented its previous value cannot be obtained. When incrementing std::istream_iterator<T>
it extracts a value into a T()
object and returns the result. Consequently, the value is discarded and cannot be re-obtained.
What you have to do is store each read into a vector and print it out afterwards. You can also use std::ostream_iterator
:
std::ostream_iterator<char> out(std::cout);
Itf ef(file);
out = ++ef;
out = ++ef;
out = ++ef;
This is also equivalent:
std::copy(ef, Itf(), out);