recently. Both the old iterator tags mechanism and the new iterator_traits mechanism are currently supported [1 , but the old iterator tag functions are no longer part of the standard C++ library and they will eventually be removed.
Example This generic function returns the last element in a non-empty range. Note that there is no way to define a function with this interface in terms of the old value_type function, because the function's return type must be declared to be the iterator's value type.
template <class InputIterator>
iterator_traits<InputIterator>::value_type last_value(InputIterator first, InputIterator last) {
iterator_traits<InputIterator>::value_type result = *first;
for (++first; first != last; ++first) result = *first;
return result;
}
(Note: this is an example of how to use iterator_traits; it is not an example of good code. There are better ways of finding the last element in a range of bidirectional iterators, or even forward iterators.)
Definition Defined in the standard header iterator, and in the nonstandard backward-compatibility header iterator.h.
Template parameters | Parameter | Description |
Iterator | The iterator type whose associated types are being accessed. |
Model of Default Constructible, Assignable
Type requirements • Iterator is a model of one of the iterator concepts. (Input Iterator, Output Iterator, Forward Iterator, Bidirectional Iterator, or Random Access Iterator.)
Public base classes None.
Members None, except for nested types.
| Member | Description |
iterator_category | One of the types input_iterator_tag, output_iterator_tag, forward_iterator_tag, bidirectional_iterator_tag, or random_access_iterator_tag. An iterator's category is the most specific iterator concept that it is a model of. |
value_type | Iterator's value type, as defined in the Trivial Iterator requirements. |
difference_type | Iterator's distance type, as defined in the Input Iterator requirements. |
pointer | Iterator's pointer type: a pointer to its value type. |
reference | Iterator's reference type: a reference to its value type. |
Notes [1] The iterator_traits class relies on a C++ feature known as partial specialization. Many of today's compilers don't implement the complete standard; in particular, many compilers do not support partial specialization. If your compiler does not support partial specialization, then you will not be able to use iterator_traits, and you will have to continue using the old iterator tag functions iterator_category, distance_type, and value_type. This is one reason that those functions have not yet been removed.
See also The iterator overview, iterator tags, input_iterator_tag, output_iterator_tag, forward_iterator_tag, bidirectional_iterator_tag, random_access_iterator_tag, input_iterator, output_iterator, forward_iterator, bidirectional_iterator, random_access_iterator
Category: iterators
Component type: function
Prototype Iterator_category is overloaded; it is in fact six different functions.
inline output_iterator_tag iterator_category(const output_iterator&);
template <class T, class Distance> inline input_iterator_tag
iterator_category(const input_iterator<T, Distance>&);
template <class T, class Distance> inline forward_iterator_tag
iterator_category(const forward_iterator<T, Distance>&);
template <class T, class Distance> inline bidirectional_iterator_tag