They are constructed from a basic_istream object, to which they become associated, so that whenever operator++ is used on the iterator, it extracts an element (with ) from the stream.
A special value for this iterator exists: the end-of-stream; When an iterator is set to this value has either reached the end of the stream (operator void* applied to the stream returns false) or has been constructed using its default constructor (without associating it with any basic_istream object).
It is defined with an operation similar to:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
The <iterator> header file also defines two overloaded global operator functions (== and !=), which compare iterators: Two istream_iterators compare equal if they are constructed from the same stream, or if they are both end-of-stream iterators (either because they reached the end of the stream or because they were default-constructed).
Template parameters
- T
- Element type for the iterator: The type of elements extracted from the stream
- charT
- First template parameter for the basic_istream: The type of elements the stream manages (char for istream).
- traits
- Second template parameter for the basic_istream: Character traits for the elements the stream manages.
- Distance
- Type to represent the difference between two iterators (generally an integral type, such as ptrdiff_t).
Member functions
- constructor
- istream_iterator objects are constructed from an istream object.
The default constructor constructs an end-of-stream iterator and the copy constructor constructs a copy of the iterator passed as argument. - operator*
- Returns the next value in the stream.
- operator->
- Returns a pointer the next value in the stream in order to access one of its members.
- operator++
- Extracts a new element from the associated istream object, unless the iterator is an end-of-stream iterator.
Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Possible output:
Please, insert two values: 2 32 2*32=64 |
See also
| ostream_iterator | Ostream iterator (class template) |
| InputIterator | Input iterator category |
| istream | Input stream (class) |
