Click on the banner to return to the user guide home page.

2.3 Stream Iterators

Stream iterators are used to access an existing input or output stream using iterator operations.

2.3.1 Input Stream Iterators

Stream Iterators

As we noted in the discussion of input iterators, the standard library provides a mechanism to turn an input stream into an input iterator. This ability is provided by the class istream_iterator. When declared, the four template arguments are the element type, the stream character type, the character traits type, and a type that measures the distance between elements. The latter two default to char_traits<charT > and ptrdiff_t. This is almost always the appropriate behavior. The single argument provided to the constructor for an istream_iterator is the stream to be accessed. Each time the ++ operator is invoked on an input stream iterator a new value from the stream is read (using the >> operator) and stored. This value is then available through the use of the dereference operator (operator *). The value constructed by istream_iterator when no arguments are provided to the constructor can be used as an ending iterator value. The following, for example, finds the first value 7 in a file of integer values.

istream_iterator<int, char> intstream(cin), eof;
istream_iterator<int, char>::iterator where = 
         find(intstream, eof, 7);

The element denoted by an iterator for an input stream is valid only until the next element in the stream is requested. Also, since an input stream iterator is an input iterator, elements can only be accessed, they cannot be modified by assignment. Finally, elements can be accessed only once, and only in a forward moving direction. If you want to read the contents of a stream more than one time, you must create a separate iterator for each pass.

2.3.2 Output Stream Iterators

The output stream iterator mechanism is analogous to the input stream iterator. Each time a value is assigned to the iterator, it will be written on the associated output stream, using the >> operator. To create an output stream iterator you must specify, as an argument with the constructor, the associated output stream. Values written to the output stream must recognize the stream >> operation. An optional second argument to the constructor is a string that will be used as a separator between each pair of values. The following, for example, copies all the values from a vector into the standard output, and separates each value by a space:

copy (newdata.begin(), newdata.end(), 
      ostream_iterator<int,char> (cout, " "));

Simple file transformation algorithms can be created by combining input and output stream iterators and the various algorithms provided by the standard library. The following short program reads a file of integers from the standard input, removes all occurrences of the value 7, and copies the remainder to the standard output, separating each value by a new line:

void main() 
{
   istream_iterator<int, char> input (cin), eof;
   ostream_iterator<int, char> output (cout, "\n");

   remove_copy (input, eof, output, 7);
}

©Copyright 1996, Rogue Wave Software, Inc.