Table of Contents
C++ Iterator library
https://en.cppreference.com/w/cpp/iterator
The iterator library provides definitions for
kinds of iterators as well as iterator traits, adaptors, and utility functions.
Iterator categories
There are
kinds of iterators:
,
,
,
,
}}.
Instead of being defined by specific types, each category of iterator is defined by the operations that can be performed on it. This definition means that any type that supports the necessary operations can be used as an iterator – for example, a pointer supports all of the operations required by
, so a pointer can be used anywhere a
is expected.
All of the iterator categories (except
) can be organized into a hierarchy, where more powerful iterator categories (e.g.
) support the operations of less powerful categories (e.g.
). If an iterator falls into one of these categories and also satisfies the requirements of
, then it is called a mutable iterator and supports both input and output. Non-mutable iterators are called constant iterators.
{]] | class="wikitable" !colspan="5" | Iterator category !Defined operations | - | rowspan="5" | {{named req | ContiguousIterator}} | rowspan="4" | {{named req | RandomAccessIterator}} | rowspan="3" | {{named req | BidirectionalIterator}} | rowspan="2" | {{named req | ForwardIterator}} | {{named req | InputIterator}} | * read * increment (without multiple passes) | - | c_plus_plus_iterator | * increment (with multiple passes) | - | colspan="2" | c_plus_plus_iterator | * decrement | - | colspan="3" | c_plus_plus_iterator | * random access | - | colspan="4" | c_plus_plus_iterator | * contiguous storage | - | " | Iterators that fall into one of the above categories and also meet the requirements of {{named req | OutputIterator}} are called mutable iterators. | - | {{named req | OutputIterator}} | colspan="4" | c_plus_plus_iterator | * write * increment (without multiple passes) | {{named req | ContiguousIterator}} category was only formally specified in C++17, but the iterators of {{lc | vector}}, {{lc | basic_string}}, {{lc | array}}, and {{lc | valarray}}, as well as pointers into C arrays are often treated as a separate category in pre-C++17 code. === C++20 iterator concepts === C++20 introduces a new system of iterators based on [[cpp/language/constraints | concepts that are different from C++17 iterators. While the basic taxonomy remains similar, the requirements for individual iterator categories are somewhat different.
Iterator associated types
Iterator primitives
Iterator customization points
Algorithm concepts and utilities
C++20 also provides a set of concepts and related utility templates designed to ease constraining common algorithm operations.
Iterator adaptors
Stream iterators
Iterator operations
Range access
These non-member functions provide a generic interface for containers, plain arrays, and
.