Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
iterator
advance
back_inserter
distance
front_inserter
inserter
iterator
iterator_traits
iterator categories:
BidirectionalIterator
ForwardIterator
InputIterator
OutputIterator
RandomAccessIterator
predefined iterators:
back_insert_iterator
front_insert_iterator
insert_iterator
istreambuf_iterator
istream_iterator
ostreambuf_iterator
ostream_iterator
reverse_iterator


iterator_traits

class template
<iterator>

Iterator traits

Standard algorithms use some members of the iterator_traits class to determine the characteristics of the iterators passed to them.

For every iterator type, a corresponding specialization of iterator_traits class template shall exist, with at least the following member types defined:

memberdescription
difference_typeType to express the result of subtracting one iterator from another
value_typeThe type of the element the iterator can point to
pointerThe type of a pointer to an element the iterator can point to
referenceThe type of a reference to an element the iterator can point to
iterator_categoryThe iterator category. It can be one of these:
  • input_iterator_tag
  • output_iterator_tag
  • forward_iterator_tag
  • bidirectional_iterator_tag
  • random_access_iterator_tag

The iterator_traits class template comes with a default definition for all iterator types that takes these member types from the iterator itself:
1
2
3
4
5
6
7
template <class Iterator> struct iterator_traits {
  typedef typename Iterator::difference_type difference_type;
  typedef typename Iterator::value_type value_type;
  typedef typename Iterator::pointer pointer;
  typedef typename Iterator::reference reference;
  typedef typename Iterator::iterator_category iterator_category;
}

Therefore, if an iterator has these member types defined does not need to explicitly specialize the iterator_traits class.

The iterator_traits class template also comes with specialized versions for pointers and pointers to const:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T> struct iterator_traits<T*> {
  typedef ptrdiff_t difference_type;
  typedef T value_type;
  typedef T* pointer;
  typedef T& reference;
  typedef random_access_iterator_tag iterator_category;
}
template <class T> struct iterator_traits<const T*> {
  typedef ptrdiff_t difference_type;
  typedef T value_type;
  typedef const T* pointer;
  typedef const T& reference;
  typedef random_access_iterator_tag iterator_category;
}

So there is no need to define specific iterator_traits specializations for pointer types.