I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlIterators

Output Iterator

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

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

Expression Description
*i=val Writes val to where the iterator refers
++i Steps 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:

Example - creation of a deque using output iterator
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
References