template <class Iterator> class iterator_traits;template <class T> class iterator_traits<T*>;template <class T> class iterator_traits<const T*>;
| 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: |
void.| 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 |
1
2
3
4
5
6
7
8
9
10
11
// iterator_traits example
#include <iostream> // std::cout
#include <iterator> // std::iterator_traits
#include <typeinfo> // typeid
int main() {
typedef std::iterator_traits<int*> traits;
if (typeid(traits::iterator_category)==typeid(std::random_access_iterator_tag))
std::cout << "int* is a random-access iterator";
return 0;
}
int* is a random-access iterator