C++ concepts: OutputIterator
An OutputIterator is an Iterator that can write to the pointed-to element.
An example of a type that implements OutputIterator is std::ostream_iterator.
When ForwardIterator, BidirectionalIterator, or RandomAccessIterator satisfies the OutputIterator requirements in addition to its own requirements, it is described as mutable.
Contents |
[edit] Requirements
The type X satisfies OutputIterator if
- The type
XsatisfiesIterator -
Xis a class type or a pointer type
And, given
-
o, a value of some type that is writable to the output iterator (there may be multiple types that are writable, e.g. ifoperator=may be a template. There is no notion ofvalue_typeas for the input iterators) -
r, an lvalue of typeX,
The following expressions must be valid and have their specified effects
| Expression | Return | Equivalent expression | Pre-condition | Post-conditions | Notes |
|---|---|---|---|---|---|
| *r = o | (not used) | r is dereferencable
|
r is incrementable
|
After this operation r is not required to be dereferenceable and any copies of the previous value of r are no longer required to be dereferenceable or incrementable.
| |
| ++r | X& | r is incrementable
|
&r == &++r, r is dereferencable or past-the-end
|
After this operation r is not required to be incrementable and any copies of the previous value of r are no longer required to be dereferenceable or incrementable.
| |
| r++ | convertible to const X& | X temp = r; ++r; |
|||
| *r++ = o | (not used) | *r = o; ++r; |
[edit] Notes
The only valid use of operator* with an output iterator is on the left of an assignment: operator* may return a proxy object, which defines a member operator= (which may be a template)
Equality and inequality may not be defined for output iterators. Even if an operator== is defined, x == y need not imply ++x == ++y.
Assignment through the same value of an output iterator happens only once: algorithms on output iterators must be single-pass algorithms.
Assignment through an output iterator is expected to alternate with incrementing. Double-increment is undefined behavior (C++ standard currently claims that double increment is supported, contrary to the STL documentation; this is LWG #2035)
Pure output-only iterator is allowed to declare its iterator_traits<X>::value_type, iterator_traits<X>::difference_type, iterator_traits<X>::pointer, and iterator_traits<X>::reference to be void (and iterators such as std::back_insert_iterator do just that).
[edit] Standard library
The following standard library iterators are output iterators that are not forward iterators:
- std::ostream_iterator
- std::ostreambuf_iterator
- std::insert_iterator
- std::back_insert_iterator
- std::front_insert_iterator