Namespaces
Variants
Views
Actions

std::input_iterator_tag, std::output_iterator_tag, std::forward_iterator_tag, std::bidirectional_iterator_tag, std::random_access_iterator_tag

From cppreference.com
< cpp‎ | iterator
Revision as of 12:21, 14 May 2013 by P12 (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">
struct input_iterator_tag { };
</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">
struct output_iterator_tag { };
</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">
struct forward_iterator_tag : public input_iterator_tag { };
</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">
struct bidirectional_iterator_tag : public forward_iterator_tag { };
</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">
struct random_access_iterator_tag : public bidirectional_iterator_tag { };
</td>

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

The empty types std::input_iterator_tag, std::output_iterator_tag, forward_iterator_tag, bidirectional_iterator_tag, and random_access_iterator_tag define the category of an iterator. Each tag corresponds to one of the five iterator categories:

Iterator category tags carry information that can be used to select the most efficient algorithms for the specific requirement set that is implied by the category.

For every iterator type, a typedef std::iterator_traits<Iterator>::iterator_category is available, which is an alias to one of these five tag types.

Example

Common technique for algorithm selection based on iterator category tags is to use a dispatcher function (the alternative is std::enable_if)

#include <iostream>
#include <vector>
#include <list>
#include <iterator>
 
template< class BDIter >
void alg(BDIter, BDIter, std::bidirectional_iterator_tag)
{
    std::cout << "alg() called for bidirectional iterator\n";
}
 
template <class RAIter>
void alg(RAIter, RAIter, std::random_access_iterator_tag)
{
    std::cout << "alg() called for random-access iterator\n";
}
 
template< class Iter >
void alg(Iter first, Iter last)
{
    alg(first, last,
        typename std::iterator_traits<Iter>::iterator_category());
}
 
int main()
{
    std::vector<int> v;
    alg(v.begin(), v.end());
 
    std::list<int> l;
    alg(l.begin(), l.end());
 
//    std::istreambuf_iterator<char> i1(std::cin), i2;
//    alg(i1, i2); // compile error: no matching function for call
}

Output:

alg() called for random-access iterator
alg() called for bidirectional iterator

See also

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