Output iterator steps forward with write access and is provided by ostream and inserter

You're viewing an older version of this page (#4212). View the current version.

View versions (1)

Description

Output iterator moves forward and may store but not retrieve values, provided by ostream and inserter.

Output iterator allows you to assign new values only element-by-element. Therefore, we can't use an output iterator to iterate twice over the same range.

Note! No comparison operations are required for output iterators. You can't check if an output iterator is valid or if a "writing" was successful.

Operations

ExpressionDescription
*i=valWrites val to where the iterator refers
++iSteps forward and returns new position (when not null)
i++Steps forward and returns old position (when not null)
TYPE(i)Copies iterator (copy constructor)

References

Example 1
Problem

The following example of program illustrates creation of a deque using output iterator.

Workings
#include <iostream>
using std::cout;
using std::endl;

#include <deque>     // deque class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator

int main()
{
   std::deque< int > values; // create deque of type int
   std::ostream_iterator< int > output(cout, " ");

   values.push_front(2);
   values.push_front(3);
   values.push_back(1);

   cout << "values contains: ";
   // use subscript operator to obtain elements of values
   for ( int i = 0; i < values.size(); i++ )
      cout << values[i] << ' ';
   cout << endl;
   return 0;
}
Solution

Output:


values contains: 3 2 1

See Also