A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../error/error_code/../../iterator.html below:

Iterator library - cppreference.com

Iterators are a generalization of pointers that allow a C++ program to work with different data structures (for example, containers and ranges(since C++20)) in a uniform manner. The iterator library provides definitions for iterators, as well as iterator traits, adaptors, and utility functions.

Since iterators are an abstraction of pointers, their semantics are a generalization of most of the semantics of pointers in C++. This ensures that every function template that takes iterators works as well with regular pointers.

[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). (See also LegacyIterator for the most basic kind of iterator.)

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.

Iterators are called constexpr iterators if all operations provided to meet iterator category requirements are constexpr functions.

(since C++20)
  1. ↑ 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.

Note: A type supporting the required operations in a row of the table above does not necessarily fall into the corresponding category, see the category page for the complete list of requirements.

[edit] Definitions [edit] Types and writability

An input iterator i supports the expression *i, resulting in a value of some object type T, called the value type of the iterator.

An output iterator i has a non-empty set of types that are writable(until C++20)indirectly_writable(since C++20) to the iterator; for each such type T, the expression *i = o is valid where o is a value of type T.

For every iterator type X for which equality is defined(until C++20), there is a corresponding signed integer(until C++20)integer-like(since C++20) type called the difference type of the iterator.

[edit] Dereferenceability and validity

Just as a regular pointer to an array guarantees that there is a pointer value pointing past the last element of the array, so for any iterator type there is an iterator value that points past the last element of a corresponding sequence. Such a value is called a past-the-end value.

Values of an iterator i for which the expression *i is defined are called dereferenceable. The standard library never assumes that past-the-end values are dereferenceable.

Iterators can also have singular values that are not associated with any sequence. Results of most expressions are undefined for singular values; the only exceptions are

In these cases the singular value is overwritten the same way as any other value. Dereferenceable values are always non-singular.

An invalid iterator is an iterator that may be singular.

[edit] Ranges

Most of the standard library’s algorithmic templates that operate on data structures have interfaces that use ranges.

An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j. If j is reachable from i, they refer to elements of the same sequence.

A range is a pair of iterators that designate the beginning and end of the computation. A range [ii) is an empty range; in general, a range [ij) refers to the elements in the data structure starting with the element pointed to by i and up to but not including the element pointed to by j.

Range [ij) is valid if and only if j is reachable from i.

(until C++20)

A range can be denoted by either

Iterator-sentinel range

An iterator and a sentinel denoting a range are comparable. [is) is empty if i == s; otherwise, [is) refers to the elements in the data structure starting with the element pointed to by i and up to but not including the element, if any, pointed to by the first iterator j such that j == s.

A sentinel s is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == s.

If s is reachable from i, [is) denotes a valid range.

Counted range

A counted range i + [​0​n) is empty if n == 0; otherwise, i + [​0​n) refers to the n elements in the data structure starting with the element pointed to by i and up to but not including the element, if any, pointed to by the result of n applications of ++i.

A counted range i + [​0​n) is valid if and only if

(since C++20)

The result of the application of functions in the standard library to invalid ranges is undefined.

[edit] Iterator concepts (since C++20)

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.

[edit] Iterator associated types (since C++20) [edit] Iterator primitives [edit] Iterator customization points (since C++20) casts the result of dereferencing an object to its associated rvalue reference type
(customization point object)[edit] swaps the values referenced by two dereferenceable objects
(customization point object)[edit] [edit] Algorithm concepts and utilities (since C++20)

A set of concepts and related utility templates designed to ease constraining common algorithm operations.

[edit] Iterator adaptors [edit] Stream iterators [edit] Iterator operations advances an iterator by given distance
(function template) [edit] returns the distance between two iterators
(function template) [edit] increment an iterator
(function template) [edit] decrement an iterator
(function template) [edit] advances an iterator by given distance or to a given bound
(algorithm function object)[edit] returns the distance between an iterator and a sentinel, or between the beginning and end of a range
(algorithm function object)[edit] increment an iterator by a given distance or to a bound
(algorithm function object)[edit] decrement an iterator by a given distance or to a bound
(algorithm function object)[edit] [edit] Range access (since C++11)

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

returns an iterator to the beginning of a container or array
(function template) [edit] returns an iterator to the end of a container or array
(function template) [edit] returns a reverse iterator to the beginning of a container or array
(function template) [edit] returns a reverse end iterator for a container or array
(function template) [edit] returns the size of a container or array
(function template) [edit] checks whether the container is empty
(function template) [edit] obtains the pointer to the underlying array
(function template) [edit] [edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4