• input_iterator
• forward_iterator
• bidirectional_iterator
• random_access_iterator
• output_iterator_tag
• input_iterator_tag
• forward_iterator_tag
• bidirectional_iterator_tag
• random_access_iterator_tag
• iterator_traits
• iterator_category
• value_type
• distance_type
[1] Output Iterators have neither a distance type nor a value type; in many ways, in fact, Output Iterators aren't really iterators. Output iterators do not have a value type, because it is impossible to obtain a value from an output iterator but only to write a value through it. They do not have a distance type, similarly, because it is impossible to find the distance from one output iterator to another. Finding a distance requires a comparison for equality, and output iterators do not support
[2] The
[3] Note that Trivial Iterator does not appear in this list. The Trivial Iterator concept is introduced solely for conceptual clarity; the STL does not actually define any Trivial Iterator types, so there is no need for a Trivial Iterator tag. There is, in fact, a strong reason not to define one: the C++ type system does not provide any way to distinguish between a pointer that is being used as a trivial iterator (that is, a pointer to an object that isn't part of an array) and a pointer that is being used as a Random Access Iterator into an array.
Input Iterator, Output Iterator, Forward Iterator, Bidirectional Iterator, Random Access Iterator,
iterator_traits<Iterator>
Category: iterators
Component type: type
As described in the Iterator Overview, one of the most important facts about iterators is that they have associated types. An iterator type, for example, has an associated
(Pointers, for example, are iterators; the value type of
Generic algorithms often need to have access to these associated types; an algorithm that takes a range of iterators, for example, might need to declare a temporary variable whose type is the iterators' value type. The class
The most obvious way to allow declarations of that sort would be to require that all iterators declare nested types; an iterator
The library contains two definitions of
The implementation of
template <class Iterator>
struct iterator_traits {
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
template <class T>
struct iterator_traits<T*> {
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};
If you are defining a new iterator type
Note that
