Iterator library
The iterator library provides definitions for five (until C++17)six (since C++17) kinds of iterators as well as iterator traits, adaptors, and utility functions.
Iterator categories
There are five (until C++17)six (since C++17) kinds of iterators: LegacyInputIterator, LegacyOutputIterator, LegacyForwardIterator, LegacyBidirectionalIterator, LegacyRandomAccessIterator, and LegacyContiguousIterator (since C++17).
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 LegacyRandomAccessIterator, so a pointer can be used anywhere a LegacyRandomAccessIterator is expected.
All of the iterator categories (except LegacyOutputIterator and LegacyContiguousIterator) can be organized into a hierarchy, where more powerful iterator categories (e.g. LegacyRandomAccessIterator) support the operations of less powerful categories (e.g. LegacyInputIterator). If an iterator falls into one of these categories and also satisfies the requirements of LegacyOutputIterator, then it is called a mutable iterator and supports both input and output. Non-mutable iterators are called constant iterators.
Iterator category | Defined operations | |||
---|---|---|---|---|
LegacyRandomAccessIterator | LegacyBidirectionalIterator | LegacyForwardIterator | LegacyInputIterator |
|
| ||||
| ||||
| ||||
Iterators that fall into one of the above categories and also meet the requirements of LegacyOutputIterator are called mutable iterators. | ||||
LegacyOutputIterator |
| |||
Iterators that fall into one of the above categories and also meet the requirements of LegacyContiguousIterator are called contiguous iterators. | ||||
LegacyContiguousIterator |
|
Note: LegacyContiguousIterator category was only formally specified in C++17, but the iterators of std::vector, std::basic_string, std::array, and std::valarray, as well as pointers into C arrays are often treated as a separate category in pre-C++17 code.
C++20 iterator conceptsC++20 introduces a new system of iterators based on 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 customization points
|
(since C++20) |
Iterator primitives
provides uniform interface to the properties of an iterator (class template) | |
empty class types used to indicate iterator categories (class) | |
(deprecated in C++17) |
base class to ease the definition of required types for simple iterators (class template) |
Iterator adaptors
iterator adaptor for reverse-order traversal (class template) | |
(C++14) |
creates a std::reverse_iterator of type inferred from the argument (function template) |
(C++11) |
iterator adaptor which dereferences to an rvalue reference (class template) |
(C++20) |
sentinel adaptor for use with std::move_iterator (class template) |
(C++11) |
creates a std::move_iterator of type inferred from the argument (function template) |
(C++20) |
adapts an iterator type and its sentinel into a common iterator type (class template) |
(C++20) |
default sentinel for use with iterators that know the bound of their range (class) |
(C++20) |
iterator adaptor that tracks the distance to the end of the range (class template) |
(C++20) |
sentinel that always compares unequal to any WeaklyIncrementable type (class) |
iterator adaptor for insertion at the end of a container (class template) | |
creates a std::back_insert_iterator of type inferred from the argument (function template) | |
iterator adaptor for insertion at the front of a container (class template) | |
creates a std::front_insert_iterator of type inferred from the argument (function template) | |
iterator adaptor for insertion into a container (class template) | |
creates a std::insert_iterator of type inferred from the argument (function template) |
Stream iterators
input iterator that reads from std::basic_istream (class template) | |
output iterator that writes to std::basic_ostream (class template) | |
input iterator that reads from std::basic_streambuf (class template) | |
output iterator that writes to std::basic_streambuf (class template) |
Iterator operations
Defined in header
<iterator> | |
advances an iterator by given distance (function template) | |
returns the distance between two iterators (function template) | |
(C++11) |
increment an iterator (function template) |
(C++11) |
decrement an iterator (function template) |
(C++20) |
advances an iterator by given distance or to a given bound (niebloid) |
(C++20) |
returns the distance between an iterator and a sentinel, or between the beginning and end of a range (niebloid) |
(C++20) |
increment an iterator by a given distance or to a bound (niebloid) |
(C++20) |
decrement an iterator by a given distance or to a bound (niebloid) |
Range access
These non-member functions provide a generic interface for containers, plain arrays, and std::initializer_list.
Defined in header
<iterator> | |
Defined in namespace
std | |
(C++11)(C++14) |
returns an iterator to the beginning of a container or array (function template) |
(C++11)(C++14) |
returns an iterator to the end of a container or array (function template) |
(C++14) |
returns a reverse iterator to a container or array (function template) |
(C++14) |
returns a reverse end iterator for a container or array (function template) |
Defined in header
<ranges> | |
Defined in namespace
std::ranges | |
returns an iterator to the beginning of a range (customization point object) | |
returns an iterator to the end of a range (customization point object) | |
returns a reverse iterator to a range (customization point object) | |
returns a reverse end iterator to a range (customization point object) |
Container access
These non-member functions provide a generic interface for containers, plain arrays, and std::initializer_list.
Defined in header
<iterator> | |
(C++17)(C++20) |
returns the size of a container or array (function template) |
(C++17) |
checks whether the container is empty (function template) |
(C++17) |
obtains the pointer to the underlying array (function template) |
Defined in header
<ranges> | |
Defined in namespace
std::ranges | |
obtains the size of a range whose size can be calculated in constant time (customization point object) | |
checks whether a range is empty (customization point object) | |
obtains a pointer to the beginning of a contiguous range (customization point object) |