Istream Iterator

istream_iterator Input Iterator

istream_iterator<T> reads (using operator>>) successive elements from the input stream for which it was constructed. After it is constructed, and every time the++  operator is used, the iterator reads and stores a value of T. If the end of stream is reached (operator void*() on the stream returns false), the iterator becomes equal to the end-of-stream iterator value. The constructor with no arguments istream_iterator() always constructs an end of stream input iterator object, which is the only legitimate iterator to be used for the end condition. The result of operator* on an end of stream is not defined. For any other iterator value a const T& is returned. It is impossible to store things into istream iterators. The main peculiarity of the istream iterators is the fact that ++ operators are not equality preserving, that is, i == j does not guarantee at all that ++i == ++j. Every time ++ is used a new value is read. (See Input iterators.)

istream_iterator () Constructor on istream_iterator
istream_iterator (istream& s) Constructor on istream_iterator
* Operator on istream_iterator
++ Operator on istream_iterator
== (const istream_iterator& y) Operator on istream_iterator

The practical consequence is that istream iterators can be used only for one-pass algorithms, since for multi-pass algorithms it is always more appropriate to use in memory data structures. Two end-of-stream iterators are always equal. An end-of-stream iterator is not equal to a non-end-of-stream iterator. Two non-end-of-stream iterators are equal when they are constructed from the same stream.


template 
class istream_iterator : public input_iterator {
    friend bool operator==(const istream_iterator& x,
                           const istream_iterator& y);
public:
    istream_iterator();
    istream_iterator(istream& s);
    istream_iterator(const istream_iterator& x);
    ~istream_iterator();
    const T& operator*() const;
    istream_iterator& operator++();
    istream_iterator operator++(int);
};

template 
bool operator==(const istream_iterator& x,
                const istream_iterator& y);