Namespaces
Variants
Views
Actions

Iterator library

From cppreference.com
< cpp
 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
Utilities
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
(C++11)
(C++11)
Range access
(C++11)(C++14)
(C++11)(C++14)
(C++17)(C++20)
(C++14)(C++14)
(C++14)(C++14)
(C++17)
(C++17)
 

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.

Contents

[edit] 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) 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
LegacyContiguousIterator LegacyRandomAccessIterator LegacyBidirectionalIterator LegacyForwardIterator LegacyInputIterator
  • read
  • increment (without multiple passes)
  • increment (with multiple passes)
  • decrement
  • random access
  • contiguous storage

Iterators that fall into one of the above categories and also meet the requirements of LegacyOutputIterator are called mutable iterators.

LegacyOutputIterator
  • write
  • increment (without multiple passes)

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.

[edit] C++20 iterator concepts

C++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.

Defined in namespace std
specifies that a type is indirectly readable by applying operator *
(concept) [edit]
specifies that a value can be written to an iterator's referenced object
(concept) [edit]
specifies that a semiregular type can be incremented with pre- and post-increment operators
(concept) [edit]
specifies that the increment operation on a weakly_incrementable type is equality-preserving and that the type is equality_comparable
(concept) [edit]
specifies that objects of a type can be incremented and dereferenced
(concept) [edit]
specifies a type is a sentinel for an input_or_output_iterator type
(concept) [edit]
specifies that the - operator can be applied to an iterator and a sentinel to calculate their difference in constant time
(concept) [edit]
specifies that a type is an input iterator, that is, its referenced values can be read and it can be both pre- and post-incremented
(concept) [edit]
specifies that a type is an output iterator for a given value type, that is, values of that type can be written to it and it can be both pre- and post-incremented
(concept) [edit]
specifies that an input_iterator is a forward iterator, supporting equality comparison and multi-pass
(concept) [edit]
specifies that a forward_iterator is a bidirectional iterator, supporting movement backwards
(concept) [edit]
specifies that a bidirectional_iterator is a random-access iterator, supporting advancement in constant time and subscripting
(concept) [edit]
specifies that a random_access_iterator is a contiguous iterator, referring to elements that are contiguous in memory
(concept) [edit]

[edit] Iterator associated types

Defined in namespace std
computes the difference type of a weakly_incrementable type
(class template) [edit]
computes the value type of an indirectly_readable type
(class template) [edit]
computes the associate types of an iterator
(alias template) [edit]

[edit] Iterator primitives

provides uniform interface to the properties of an iterator
(class template) [edit]
empty class types used to indicate iterator categories
(class) [edit]
(deprecated in C++17)
base class to ease the definition of required types for simple iterators
(class template) [edit]

[edit] Iterator customization points

Defined in namespace std::ranges
(C++20)
casts the result of dereferencing an object to its associated rvalue reference type
(customization point object) [edit]
(C++20)
swap the values referenced by two dereferenceable objects
(customization point object) [edit]

[edit] Algorithm concepts and utilities

C++20 also provides a set of concepts and related utility templates designed to ease constraining common algorithm operations.

Defined in header <iterator>
Defined in namespace std
Indirect callable concepts
specifies that a callable type can be invoked with the result of dereferencing an indirectly_readable type
(concept) [edit]
specifies that a callable type, when invoked with the result of dereferencing an indirectly_readable type, satisfies predicate
(concept) [edit]
specifies that a callable type, when invoked with the result of dereferencing two indirectly_readable types, satisfies predicate
(concept) [edit]
specifies that a callable type, when invoked with the result of dereferencing two indirectly_readable types, satisfies equivalence_relation
(concept) [edit]
specifies that a callable type, when invoked with the result of dereferencing two indirectly_readable types, satisfies strict_weak_order
(concept) [edit]
Common algorithm requirements
specifies that values may be moved from an indirectly_readable type to an indirectly_writable type
(concept) [edit]
specifies that values may be moved from an indirectly_readable type to an indirectly_writable type and that the move may be performed via an intermediate object
(concept) [edit]
specifies that values may be copied from an indirectly_readable type to an indirectly_writable type
(concept) [edit]
specifies that values may be copied from an indirectly_readable type to an indirectly_writable type and that the copy may be performed via an intermediate object
(concept) [edit]
specifies that the values referenced by two indirectly_readable types can be swapped
(concept) [edit]
specifies that the values referenced by two indirectly_readable types can be compared
(concept) [edit]
specifies the common requirements of algorithms that reorder elements in place
(concept) [edit]
(C++20)
specifies the requirements of algorithms that merge sorted sequences into an output sequence by copying elements
(concept) [edit]
(C++20)
specifies the common requirements of algorithms that permute sequences into ordered sequences
(concept) [edit]
Utilities
computes the result of invoking a callable object on the result of dereferencing some set of indirectly_readable types
(alias template) [edit]
(C++20)
helper template for specifying the constraints on algorithms that accept projections
(class template) [edit]

[edit] Iterator adaptors

iterator adaptor for reverse-order traversal
(class template) [edit]
creates a std::reverse_iterator of type inferred from the argument
(function template) [edit]
iterator adaptor which dereferences to an rvalue reference
(class template) [edit]
sentinel adaptor for use with std::move_iterator
(class template) [edit]
creates a std::move_iterator of type inferred from the argument
(function template) [edit]
adapts an iterator type and its sentinel into a common iterator type
(class template) [edit]
default sentinel for use with iterators that know the bound of their range
(class) [edit]
iterator adaptor that tracks the distance to the end of the range
(class template) [edit]
sentinel that always compares unequal to any weakly_incrementable type
(class) [edit]
iterator adaptor for insertion at the end of a container
(class template) [edit]
creates a std::back_insert_iterator of type inferred from the argument
(function template) [edit]
iterator adaptor for insertion at the front of a container
(class template) [edit]
creates a std::front_insert_iterator of type inferred from the argument
(function template) [edit]
iterator adaptor for insertion into a container
(class template) [edit]
creates a std::insert_iterator of type inferred from the argument
(function template) [edit]

[edit] Stream iterators

input iterator that reads from std::basic_istream
(class template) [edit]
output iterator that writes to std::basic_ostream
(class template) [edit]
input iterator that reads from std::basic_streambuf
(class template) [edit]
output iterator that writes to std::basic_streambuf
(class template) [edit]

[edit] Iterator operations

Defined in header <iterator>
advances an iterator by given distance
(function template) [edit]
returns the distance between two iterators
(function template) [edit]
(C++11)
increment an iterator
(function template) [edit]
(C++11)
decrement an iterator
(function template) [edit]
advances an iterator by given distance or to a given bound
(niebloid) [edit]
returns the distance between an iterator and a sentinel, or between the beginning and end of a range
(niebloid) [edit]
increment an iterator by a given distance or to a bound
(niebloid) [edit]
decrement an iterator by a given distance or to a bound
(niebloid) [edit]

[edit] Range access

These non-member functions provide a generic interface for containers, plain arrays, and std::initializer_list.

Defined in header <array>
Defined in header <deque>
Defined in header <forward_list>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span>
Defined in header <string>
Defined in header <string_view>
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
Defined in namespace std
(C++11)(C++14)
returns an iterator to the beginning of a container or array
(function template) [edit]
(C++11)(C++14)
returns an iterator to the end of a container or array
(function template) [edit]
returns a reverse iterator to a container or array
(function template) [edit]
(C++14)
returns a reverse end iterator for a container or array
(function template) [edit]
(C++17)(C++20)
returns the size of a container or array
(function template) [edit]
(C++17)
checks whether the container is empty
(function template) [edit]
(C++17)
obtains the pointer to the underlying array
(function template) [edit]