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
- http://www.tenouk.com/
- Nicolai M. Josuttis: "The C++ Standard Library"
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
- http://www.codecogs.com/reference/computing/stl/iterators/input_iterator.php "Input Iterator"
- http://codecogs.com/reference/computing/stl/iterators/forward_iterator.php"Forward Iterator"
- http://codecogs.com/reference/computing/stl/iterators/bidirectional_iterator.php"Bidirectional Iterator"
- http://codecogs.com/reference/computing/stl/iterators/random_access_iterator.php"Random Access Iterator"