Namespaces
Variants
Views
Actions

std::iterator_traits

From cppreference.com
< cpp‎ | iterator
Revision as of 16:31, 9 December 2012 by SuperBoi45 (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <iterator>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class Iterator>
struct iterator_traits;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class T >
struct iterator_traits<T*>;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class T >
struct iterator_traits<const T*>;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

std::iterator_traits is the type trait class that provides uniform interface to the properties of iterator types. This makes it possible to implement algorithms only in terms of iterators.

Contents

Member types

Member type Definition
difference_type Iterator::difference_type
value_type Iterator::value_type
pointer Iterator::pointer
reference Iterator::reference
iterator_category Iterator::iterator_category

Specializations

This type trait may be specialized for user-provided types that may be used as iterators. The standard library provides two partial specializations for pointer types T*, which makes it possible to use all iterator-based algorithms with raw pointers.

T* specialization member types

Member type Definition
difference_type std::ptrdiff_t
value_type T
pointer T*
reference T&
iterator_category std::random_access_iterator_tag

const T* specialization member types

Member type Definition
difference_type std::ptrdiff_t
value_type T
pointer const T*
reference const T&
iterator_category std::random_access_iterator_tag

Example

general-purpose reverse() implementation for bidirectional iterators

#include <iostream>
#include <iterator>
#include <vector>
#include <list>
 
template<class BDIter>
void my_reverse(BDIter first, BDIter last)
{
    typename std::iterator_traits<BDIter>::difference_type n = std::distance(first, last);
    --n;
    while(n > 0) {
        typename std::iterator_traits<BDIter>::value_type tmp = *first;
        *first++ = *--last;
        *last = tmp;
        n -= 2;
    }
}
 
int main()
{
    std::vector<int> v{1, 2, 3, 4, 5};
    my_reverse(v.begin(), v.end());
    for (int n : v)
        std::cout << n << ' ';
    std::cout << '\n';
 
    std::list<int> l{1, 2, 3, 4, 5};
    my_reverse(l.begin(), l.end());
    for (auto n : l)
        std::cout << n << ' ';
    std::cout << '\n';
 
//    std::istreambuf_iterator<char> i1(std::cin), i2;
//    my_reverse(i1, i2); // compilation error
 
}

Output:

5 4 3 2 1
5 4 3 2 1

See also

Template:cpp/iterator/dcl list iteratorTemplate:cpp/iterator/dcl list iterator tags