istream_iterator

this is my program,i just define the in ,haven't use it,but,after compiled,i execute the result, the program ask me input something. i have no idea...who can help me , tell me why?
best wishes!
1
2
3
4
5
6
7
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
    istream_iterator<int> in(cin);
}
Last edited on
The iterator does not refer to the end of the stream, so it must be dereferenceable. Therefore, it must grab the first character from the stream.
> just define the in ,haven't use it,but,after compiled,
> i execute the result, the program ask me input something.

The actual read operation is performed when the iterator is incremented, not when it is dereferenced. The first object may be read when the iterator is constructed or when the first dereferencing is done.
http://en.cppreference.com/w/cpp/iterator/istream_iterator
This seems fishy:
cppreference wrote:
or when the first dereferencing is done.
What if it was constructed on a stream that was already at the end but the eof flag had not been set? Could the iterator compare equal to the end iterator? If so, would that mean the equality comparison would attempt to fetch from the stream, meaning the operator cannot operate on const objects?
> What if it was constructed on a stream that was already at the end but the eof flag had not been set?
> Could the iterator compare equal to the end iterator?

My understanding is that in an implementation where the constructor does not read the first object, it would do a peek().
Ah, that makes sense. I forgot the semantics of peek.
Topic archived. No new replies allowed.