Top of document
©Copyright 1999 Rogue Wave Software

istream_iterator


     Iterators

Summary

Stream iterator that provides iterator capabilities for istreams. This iterator allows generic algorithms to be used directly on streams.

Contents

Synopsis

#include <iterator>
template <class T, class Distance = ptrdiff_t>
class istream_iterator : public input_iterator;

Description

Stream iterators provide the standard iterator interface for input and output streams.

The class istream_iterator reads elements from an input stream (using operator >>). A value of type T is retrieved and stored when the iterator is constructed and each time operator++ is called. The iterator will be equal to the end-of-stream iterator value if the end-of-file is reached. Use the constructor with no arguments to create an end-of-stream iterator. The only valid use of this iterator is to compare to other iterators when checking for end of file. Do not attempt to dereference the end-of-stream iterator; it plays the same role as the past-the-end iterator provided by the end() function of containers. Since an istream_iterator is an input iterator, you cannot assign to the value returned by dereferencing the iterator. This also means that istream_iterators can only be used for single pass algorithms.

Since a new value is read every time the operator++ is used on an istream_iterator, that operation is not equality-preserving. This means that i == j does not mean that ++i == ++j (although two end-of-stream iterators are always equal).

Interface

template <class T, class Distance = ptrdiff_t>
 class istream_iterator : public input_iterator<T, Distance>
 {
 public:
    istream_iterator();
    istream_iterator (istream&);
    istream_iterator (const istream_iterator <T, Distance>&);
    ~istream_itertor ();
    const T& operator*() const;
    const T* operator ->() const;
    istream_iterator <T, Distance>& operator++();
    istream_iterator <T, Distance>  operator++ (int)
 };
// Non-member Operators
template <class T, class Distance>
bool operator== (const istream_iterator<T, Distance>&,
                 const istream_iterator<T, Distance>&);

Constructors

istream_iterator ();
istream_iterator (istream& s);
istream_iterator  (const  istream_iterator<T,  Distance>& x);

Destructors

~istream_iterator ();

Operators

const T& 
operator* () const;
const T*
operator-> () const;
istream_iterator<T, Distance>& 
operator++ ()
istream_iterator<T, Distance> 
operator++ (int)

Non-member Operators

bool 
operator== (const istream_iterator<T, Distance>& x,
            const istream_iterator<T, Distance>& y)

Example

//
// io_iter.cpp
//
 #include <iterator>
 #include <vector>
 #include <numeric>
 #include <iostream.h>
 int main ()
 {
   vector<int> d;
   int total = 0;
   //
   // Collect values from cin until end of file
   // Note use of default constructor to get ending iterator
   //
   cout << "Enter a sequence of integers (eof to quit): " ;
   copy(istream_iterator<int,vector<int>::difference_type>(cin),
        istream_iterator<int,vector<int>::difference_type>(),
        inserter(d,d.begin()));
   //
   // stream the whole vector and the sum to cout
   //
   copy(d.begin(),d.end()-1,ostream_iterator<int>(cout," + "));
   if (d.size())
     cout << *(d.end()-1) << " = " <<
          accumulate(d.begin(),d.end(),total) << endl;
   return 0;
 }

Warning

If your compiler does not support default template parameters, then you will need to always supply the Allocator template argument. For instance, you'll have to write :

vector<int, allocator>

instead of :

vector<int>

See Also

Iterators, ostream_iterator


Top of document