Sunday, January 8, 2012

stream iterator

Stream Iterators C++

Another very helpful kind of iterator adapter is a stream iterator. Stream iterators are iterators that read from and write to a stream. [8] Thus, they provide an abstraction that lets the input from the keyboard behave as a collection, from which you can read. Similarly you can redirect the output of an algorithm directly into a file or onto the screen.

[8] A stream is an object that represents I/O channels.

Consider the following example. It is a typical example of the power of the whole STL. Compared with ordinary C or C++, it does a lot of complex processing by using only a few statements:

The program has only three statements that read all words from the standard input and print a sorted list of them. Let's consider the three statements step-by-step. In the statement

copy (istream_iterator<string>(cin),


two input stream iterators are used:

1. The expression


creates a stream iterator that reads from the standard input stream cin.[9] The template argument string specifies that the stream iterator reads elements of this type. These elements are read with the usual input operator >>. Thus, each time the algorithm wants to process the next element, the istream iterator transforms that desire into a call of

[9] In older systems you must use ptrdiff_t as the second template parameter to create an istream iterator.

cin >> string

The input operator for strings usually reads one word separated by whitespaces, so the algorithm reads word-by-word.

2. The expression


calls the default constructor of istream iterators that creates an end-of-stream iterator. It represents a stream from which you can no longer read. As usual, the copy() algorithm operates as long as the (incremented) first argument differs from the second argument. The end-of-stream iterator is used as the end of the range, so the algorithm reads all strings from cin until it can no longer read any more (due to end-of-stream or an error). To summarize, the source of the algorithm is "all words read from cin." These words are copied by inserting them into coll with the help of a back inserter. 

The sort() algorithm sorts all elements:

sort (coll.begin(), coll.end());
Lastly, the statement
unique_copy (coll.begin(), coll.end(),

copies all elements from the collection into the destination cout. During the process, the unique_copy() algorithm eliminates adjacent duplicate values. The expression


creates an output stream iterator that writes strings to cout by calling operator >> for each element. The second argument behind cout serves as a separator between the elements. It is optional. In this example, it is a newline, so every element is written on a separate line.

All components of the program are templates, so you can change the program easily to sort other value types, such as integers or more complex objects. 

In this example, one declaration and three statements were used to sort all words from standard input. However, you could do the same by using only one declaration and one statement.

See Also:

No comments:

Post a Comment