Output iterators are one of the five iterator categories in the C++ Standard Library and are designed for writing values to a sequence. They provide the minimum functionality required to assign values while traversing a container or output stream.
- Support single-pass, forward-only traversal for writing operations.
- Provide write-only access to elements without allowing value retrieval.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v(5);
auto it = v.begin();
for (int i = 1; i <= 5; ++i) {
*it = i * 10;
++it;
}
for (auto x : v)
cout << x << " ";
return 0;
}
Output
10 20 30 40 50
Explanation: Output iterators allow values to be assigned sequentially to a container but are not intended for reading values.
Output Iterators in Iterator Hierarchy
Output iterators are considered complementary to input iterators.

- Input iterators support reading but not writing.
- Output iterators support writing but not reading.
- Forward, Bidirectional, and Random - access iterators satisfy the requirements of both input and output iterators, making them more powerful than output iterators.
Features of Output Iterators
Output iterators support only those operations that are necessary for writing values sequentially to a range.
1. Single-Pass Traversal
Output iterators can be used only with single-pass algorithms where each element is written at most once.
2. Write Access
Output iterators can be dereferenced only as an lvalue to assign values.
*it = value;
it->member = value;
3. Incrementable
Output iterators support both pre-increment and post-increment operations.
++it;
it++;
4. Forward-Only Movement
Output iterators can move only in the forward direction.
Practical Use of Output Iterators
Output iterators are mainly used in algorithms that produce or copy data.
Example: std::move()
The std::move() algorithm uses output iterators to store moved elements.
template<class InputIterator,
class OutputIterator>
OutputIterator move(InputIterator first,
InputIterator last,
OutputIterator result)
{
while (first != last) {
*result = std::move(*first);
++result;
++first;
}
return result;
}
Here:
- first and last are input iterators because elements are read.
- result is an output iterator because elements are written.
Example: std::find()
The std::find() algorithm does not use output iterators because it only reads values.
template<class InputIterator, class T>
InputIterator find(InputIterator first,
InputIterator last,
const T& val)
{
while (first != last) {
if (*first == val)
return first;
++first;
}
return last;
}
Limitations of Output Iterators
Output iterators are the weakest iterator category for writing operations and have several limitations.
1. Write Only
Output iterators can assign values but cannot retrieve them.
*it = 10; // Valid
int x = *it; // Not allowed
2. No Decrement Operation
Output iterators cannot move backward.
it--; // Not allowed
3. Not Suitable for Multi-Pass Algorithms
Since output iterators support only single-pass traversal, they cannot be used in algorithms requiring multiple passes.
4. No Comparison Operations
Output iterators do not support equality or relational comparisons.
it1 == it2; // Not allowed
it1 < it2; // Not allowed
5. No Arithmetic Operations
Output iterators do not support arithmetic operations.
it + 1; // Not allowed
it - 2; // Not allowed
Common Output Iterator Adapters
The STL provides several output iterator adapters:
- std::back_inserter() - inserts elements at the end of a container.
- std::front_inserter() - inserts elements at the front of a container.
- std::inserter() - inserts elements at a specified position.
- std::ostream_iterator - writes values directly to an output stream.