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

From cppreference.com
< cpp‎ | iterator

Defined in header `<iterator>` |  |
struct input_iterator_tag { }; |  |
struct output_iterator_tag { }; |  |
struct forward_iterator_tag : public input_iterator_tag { }; |  |
struct bidirectional_iterator_tag : public forward_iterator_tag { }; |  |
struct random_access_iterator_tag : public bidirectional_iterator_tag { }; |  |
struct contiguous_iterator_tag: public random_access_iterator_tag { }; |  | (since C++20)

Defines the category of an iterator. Each tag is an empty type and corresponds to one of the five (until C++20)six (since C++20) iterator categories:

- `input_iterator_tag` corresponds to LegacyInputIterator.
- `output_iterator_tag` corresponds to LegacyOutputIterator.
- `forward_iterator_tag` corresponds to LegacyForwardIterator.
- `bidirectional_iterator_tag` corresponds to LegacyBidirectionalIterator.
- `random_access_iterator_tag` corresponds to LegacyRandomAccessIterator.

- `contiguous_iterator_tag` corresponds to LegacyContiguousIterator.

(since C++20)

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 `It`, a typedef std::iterator_traits<It>::iterator_category is available, which is an alias to one of these five (until C++20)six (since C++20) tag types.

Additionally, std::iterator_traits<It>::iterator_concept may be used to indicate conformance to the iterator concepts. | (since C++20)

### Notes

There is no separate tag for LegacyContiguousIterator.

(since C++17)
(until C++20)

### Example

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

Run this code

    #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

iterator (deprecated in C++17) | base class to ease the definition of required types for simple iterators (class template)
iterator_traits | provides uniform interface to the properties of an iterator (class template)

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/iterator_tags&oldid=110501"