Iterator categories
Iterator categories classify what an iterator can do: read, write, go forward, go backward, or jump by n. Algorithms require a minimum category. std::sort needs random access; std::find needs only a single pass forward.
Detailed Explanation
Not every sequence supports it + 100. A list must walk 100 nodes. An input stream can only read the next character once. The STL named those differences so a single std::sort could static_assert (or SFINAE) its way out of a list instead of generating nonsense.
The classical five, from weakest to strongest:
| Category | Can do | Typical source |
|---|---|---|
| Input | One-pass read, ++, * | istream_iterator |
| Output | One-pass write, ++, * | ostream_iterator, back_inserter |
| Forward | Multi-pass, ++, * | forward_list, some hash-table iterators |
| Bidirectional | Forward plus -- | list, map, set |
| Random access | Bidirectional plus + n, -, <, [] | vector, deque, array, pointers |
C++17 added contiguous as a refinement of random access: elements sit in one block, so &*(it + n) == &*it + n. vector, array, and string are contiguous. deque is random-access but not contiguous.
C++20 recast the categories as concepts (std::forward_iterator, std::random_access_iterator, std::contiguous_iterator) and split input into input_iterator versus indirectly_readable plus sentinels. You will still see the old tag types (std::random_access_iterator_tag) in pre-C++20 code and in iterator_traits.
Why it matters when you write an algorithm: if you only ++ and *, you can take a forward iterator and your function works on list and vector. If you binary-search, you need random access (or you write a different algorithm for bidirectional iterators). std::distance is O(1) for random access and O(n) otherwise. std::advance(it, n) is a loop of ++ on a list and a single add on a vector.
iterator_traits<It>::iterator_category is how generic code branches. if constexpr (std::random_access_iterator<It>) is the C++20 spelling.
Output iterators are write-only and single-pass. *it = x; ++it; is the protocol. You cannot read back. std::back_inserter(v) is an output iterator that calls push_back.
A pointer T* is a contiguous iterator. That is why C arrays work with std::sort(a, a + n).
Do not claim your iterator is random-access if + n walks a linked structure. The tag is a promise. Breaking it makes std::sort take quadratic time or worse.
Code Examples
#include <iostream>
#include <iterator>
#include <list>
#include <type_traits>
#include <vector>
template <typename It>
void classify(It) {
using Cat = typename std::iterator_traits<It>::iterator_category;
if constexpr (std::is_same_v<Cat, std::random_access_iterator_tag>) {
std::cout << "random\n";
} else if constexpr (std::is_same_v<Cat, std::bidirectional_iterator_tag>) {
std::cout << "bidi\n";
}
}
int main() {
std::vector<int> v;
std::list<int> lst;
classify(v.begin()); // prints: random
classify(lst.begin()); // prints: bidi
}#include <algorithm>
#include <vector>
int main() {
std::vector<int> out;
std::fill_n(std::back_inserter(out), 3, 7); // output iterator
// out is {7, 7, 7}
}Common Gotchas
`deque` is random-access but not contiguous
`&d[0] + i` is not `&d[i]` once you leave a chunk.
A mismatched category tag is silent UB for algorithms
tag what you actually implement.