class template
<iterator>
std::iterator_traits
template <class Iterator> class iterator_traits;template <class T> class iterator_traits<T*>;template <class T> class iterator_traits<const T*>;
Iterator traits
Standard algorithms determine certain properties of the iterators passed to them and the range they represent by using the members of the corresponding iterator_traits instantiation.
For every iterator type, a corresponding specialization of iterator_traits class template shall be defined, with at least the following member types defined:
| member | description |
|---|---|
| difference_type | Type to express the result of subtracting one iterator from another. |
| value_type | The type of the element the iterator can point to. |
| pointer | The type of a pointer to an element the iterator can point to. |
| reference | The type of a reference to an element the iterator can point to. |
| iterator_category | The iterator category. It can be one of these: |
Note: For output iterators that are not at least forward iterators, any of these member types (except for iterator_category) may be defined as
void.The iterator_traits class template comes with a default definition that obtains these types from the iterator type itself (see below). It is also specialized for pointers (T*) and pointers to const (const T*).
Note that any custom class will have a valid instantiation of iterator_traits if it publicly inherits the base class std::iterator.
Member types
| member | generic definition | T* specialization | const T* specialization |
|---|---|---|---|
| difference_type | Iterator::difference_type | ptrdiff_t | ptrdiff_t |
| value_type | Iterator::value_type | T | T |
| pointer | Iterator::pointer | T* | const T* |
| reference | Iterator::reference | T& | const T& |
| iterator_category | Iterator::iterator_category | random_access_iterator_tag | random_access_iterator_tag |
Example
|
|
Output:
int* is a random-access iterator
See also
- iterator
- Iterator base class (class template)