Name
iterator_traits class template — Iterator traits
Synopsis
template<typename 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; };
The iterator_traits
class
template declares traits for an iterator. If you use the iterator
class template as the base for
your custom iterator, you don’t need to specialize iterator_traits
. If you are writing a
custom container or algorithm, you should always use iterator_traits
to obtain the traits of an
iterator. If you use a plain pointer as an iterator, the standard
library specializes iterator_traits
for you. See the next
subsection.
If you write your own specialization, the iterator_category
type must be one of the
five iterator tag classes. (See the iterator
class template.) For an output
iterator, difference_type
and
value_type
are void
.
When writing a generic algorithm or other function that uses
iterators, you can use iterator_traits
to specialize the behavior
for certain kinds of iterators. See Example 13-19 (under distance
), which shows how iterator traits
can be used to improve the performance of a function.
See Also
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.