|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
The ranges library is an extension and generalization of the algorithms and iterator libraries that makes them more powerful by making them composable and less error-prone.
The library creates and manipulates rangeviews, lightweight objects that indirectly represent iterable sequences (ranges). Ranges are an abstraction on top of
[
begin,
end)
– iterator pairs, e.g. ranges made by implicit conversion from containers. All algorithms that take iterator pairs now have overloads that accept ranges (e.g.ranges::sort) +
[
0,
size)
– counted sequences, e.g. range returned byviews::counted[
begin,
predicate)
– conditionally-terminated sequences, e.g. range returned byviews::take_while[
begin,
..)
– unbounded sequences, e.g. range returned byviews::iotaThe ranges library includesrange algorithms, which are applied to ranges eagerly, andrange adaptors, which are applied to views lazily. Adaptors can be composed into pipelines, so that their actions take place as the view is iterated.
Defined in header <ranges> | ||
namespace std{ namespace views= ranges::views; | (since C++20) | |
The namespace aliasstd::views
is provided as a shorthand forstd::ranges::views
.
Defined in namespace std::ranges | ||
Range access | ||
Defined in header <ranges> | ||
Defined in header <iterator> | ||
(C++20) | returns an iterator to the beginning of a range (customization point object)[edit] | |
(C++20) | returns a sentinel indicating the end of a range (customization point object)[edit] | |
(C++20) | returns an iterator to the beginning of a read-only range (customization point object)[edit] | |
(C++20) | returns a sentinel indicating the end of a read-only range (customization point object)[edit] | |
(C++20) | returns a reverse iterator to a range (customization point object)[edit] | |
(C++20) | returns a reverse end iterator to a range (customization point object)[edit] | |
(C++20) | returns a reverse iterator to a read-only range (customization point object)[edit] | |
(C++20) | returns a reverse end iterator to a read-only range (customization point object)[edit] | |
(C++26) | returns an integer equal to the reserve hint given by a range (customization point object)[edit] | |
(C++20) | returns an integer equal to the size of a range (customization point object)[edit] | |
(C++20) | returns a signed integer equal to the size of a range (customization point object)[edit] | |
(C++20) | checks whether a range is empty (customization point object)[edit] | |
(C++20) | obtains a pointer to the beginning of a contiguous range (customization point object)[edit] | |
(C++20) | obtains a pointer to the beginning of a read-only contiguous range (customization point object)[edit] | |
Range primitives | ||
Defined in header <ranges> | ||
(C++20)(C++23)(C++20)(C++23) | obtains iterator and sentinel types of a range (alias template)[edit] | |
(C++20)(C++20)(C++20) | obtains size, difference, and value types of a range (alias template)[edit] | |
obtains reference types of a range (alias template)[edit] | ||
Dangling iterator handling | ||
Defined in header <ranges> | ||
(C++20) | a placeholder type indicating that an iterator or asubrange should not be returned since it would be dangling(class)[edit] | |
obtains iterator type orsubrange type of aborrowed_range (alias template)[edit] | ||
Other utilities | ||
Defined in header <ranges> | ||
(C++23) | tags a range to be treated as a sequence rather than a single value (class template)[edit] | |
Range concepts | ||
Defined in header <ranges> | ||
(C++20) | specifies that a type is a range, that is, it provides abegin iterator and anend sentinel(concept)[edit] | |
(C++20) | specifies that a type is arange and iterators obtained from an expression of it can be safely returned without danger of dangling(concept)[edit] | |
specifies that a range can estimate its size in constant time (concept)[edit] | ||
(C++20) | specifies that a range knows its size in constant time (concept)[edit] | |
(C++20) | specifies that a range is a view, that is, it has constant time copy/move/assignment (concept)[edit] | |
(C++20) | specifies a range whose iterator type satisfiesinput_iterator (concept)[edit] | |
(C++20) | specifies a range whose iterator type satisfiesoutput_iterator (concept)[edit] | |
(C++20) | specifies a range whose iterator type satisfiesforward_iterator (concept)[edit] | |
(C++20) | specifies a range whose iterator type satisfiesbidirectional_iterator (concept)[edit] | |
(C++20) | specifies a range whose iterator type satisfiesrandom_access_iterator (concept)[edit] | |
(C++20) | specifies a range whose iterator type satisfiescontiguous_iterator (concept)[edit] | |
(C++20) | specifies that a range has identical iterator and sentinel types (concept)[edit] | |
(C++20) | specifies the requirements for arange to be safely convertible to aview (concept)[edit] | |
(C++23) | specifies that a range has read-only elements (concept)[edit] | |
Range conversions | ||
Defined in header <ranges> | ||
(C++23) | constructs a new non-view object from an input range (function template)[edit] | |
Views | ||
Defined in header <ranges> | ||
(C++20) | helper class template for defining aview , using thecuriously recurring template pattern(class template)[edit] | |
(C++20) | combines an iterator-sentinel pair into aview (class template)[edit] |
Defined in header <ranges> | |
Defined in namespace std::ranges | |
an emptyview with no elements(class template)(variable template)[edit] | |
aview that contains a single element of a specified value(class template)(customization point object)[edit] | |
(C++20) | aview consisting of a sequence generated by repeatedly incrementing an initial value(class template)(customization point object)[edit] |
aview consisting of a generated sequence by repeatedly producing the same value(class template)(customization point object)[edit] | |
aview consisting of the elements obtained by successive application ofoperator>> on the associated input stream(class template)(customization point object)[edit] |
Defined in header <ranges> | |
Defined in namespace std::ranges | |
helper base class template for defining a range adaptor closure object (class template)[edit] | |
(C++20) | aview that includes all elements of arange (alias template)(range adaptor object)[edit] |
(C++20) | aview of the elements of some otherrange (class template)[edit] |
(C++20) | aview with unique ownership of somerange (class template)[edit] |
aview of a sequence that casts each element to an rvalue(class template)(range adaptor object)[edit] | |
aview that consists of the elements of arange that satisfies a predicate(class template)(range adaptor object)[edit] | |
aview of a sequence that applies a transformation function to each element(class template)(range adaptor object)[edit] | |
(C++20) | aview consisting of the first N elements of anotherview (class template)(range adaptor object)[edit] |
aview consisting of the initial elements of anotherview , until the first element on which a predicate returnsfalse(class template)(range adaptor object)[edit] | |
(C++20) | aview consisting of elements of anotherview , skipping the first N elements(class template)(range adaptor object)[edit] |
aview consisting of the elements of anotherview , skipping the initial subsequence of elements until the first element where the predicate returnsfalse(class template)(range adaptor object)[edit] | |
(C++20) | aview consisting of the sequence obtained from flattening aview ofrange s(class template)(range adaptor object)[edit] |
aview consisting of the sequence obtained from flattening a view of ranges, with the delimiter in between elements(class template)(range adaptor object)[edit] | |
aview over the subranges obtained from splitting anotherview using a delimiter(class template)(range adaptor object)[edit] | |
aview over the subranges obtained from splitting anotherview using a delimiter(class template)(range adaptor object)[edit] | |
aview consisting of concatenation of the adapted views(class template)(customization point object)[edit] | |
(C++20) | creates a subrange from an iterator and a count (customization point object)[edit] |
converts aview into acommon_range (class template)(range adaptor object)[edit] | |
aview that iterates over the elements of another bidirectional view in reverse order(class template)(range adaptor object)[edit] | |
converts aview into aconstant_range (class template)(range adaptor object)[edit] | |
takes aview consisting oftuple-like values and a number N and produces aview of Nth element of each tuple(class template)(range adaptor object)[edit] | |
(C++20) | takes aview consisting of pair-like values and produces aview of the first elements of each pair(class template)(range adaptor object)[edit] |
takes aview consisting of pair-like values and produces aview of the second elements of each pair(class template)(range adaptor object)[edit] | |
aview that maps each element of adapted sequence to a tuple of both the element's position and its value(class template)(range adaptor object)[edit] | |
(C++23) | aview consisting of tuples of references to corresponding elements of the adapted views(class template)(customization point object)[edit] |
aview consisting of results of application of a transformation function to corresponding elements of the adapted views(class template)(customization point object)[edit] | |
aview consisting of tuples of references to adjacent elements of the adapted view(class template)(range adaptor object)[edit] | |
aview consisting of results of application of a transformation function to adjacent elements of the adapted view(class template)(range adaptor object)[edit] | |
a range ofview s that areN -sized non-overlapping successive chunks of the elements of anotherview (class template)(range adaptor object)[edit] | |
aview whose Mth element is aview over the Mth through (M + N - 1)th elements of anotherview (class template)(range adaptor object)[edit] | |
splits theview into subranges between each pair of adjacent elements for which the given predicate returnsfalse(class template)(range adaptor object)[edit] | |
aview consisting of elements of anotherview , advancing over N elements at a time(class template)(range adaptor object)[edit] | |
aview consisting of tuples of results calculated by the n-ary cartesian product of the adapted views(class template)(customization point object)[edit] | |
aview that caches the last-accessed element of its underlying sequence(class template)(range adaptor object)[edit] | |
converts aview into a range that isinput_range -only and non-common_range (class template)(range adaptor object)[edit] |
Defined in header <generator> | |
Defined in namespace std | |
(C++23) | Aview that represents synchronouscoroutine generator(class template)[edit] |
SeeRangeAdaptorObject (RAO).
SeeRangeAdaptorClosureObject (RACO).
SeeCustomization point object (CPO).
Some range adaptors wrap their elements or function objects with thecopyable-box
(until C++23)movable-box
(since C++23). The wrapper augments the wrapped object with assignability when needed.
Some range adaptors are specified in terms of an exposition-only class templatenon-propagating-cache
, which behaves almost likestd::optional<T> (see description for differences).
const
typetemplate<bool Const,class T> using/*maybe-const*/=std::conditional_t<Const,const T, T>; | (exposition only*) | |
The alias template/*maybe-const*/ is a shorthand used to conditionally apply aconst qualifier to the typeT
.
template</*is-integer-like*/ T> using/*make-signed-like-t*/<T>=/* see description */; | (1) | (exposition only*) |
template</*is-integer-like*/ T> using/*make-unsigned-like-t*/<T>=/* see description */; | (2) | (exposition only*) |
template</*is-integer-like*/ T> /*make-unsigned-like-t*/<T>/*to-unsigned-like*/( T t) | (3) | (exposition only*) |
T
:T
is an integer type,/*make-signed-like-t*/<T> isstd::make_signed_t<T>.T
.T
:T
is an integer type,/*make-unsigned-like-t*/<T> isstd::make_unsigned_t<T>.T
.template<ranges::input_range R> constexprauto&/*possibly-const-range*/(R& r)noexcept | (1) | (exposition only*) |
template<class T> constexprauto/*as-const-pointer*/(const T* p)noexcept | (2) | (exposition only*) |
Some range access customization point objects are specified in terms of these exposition-only function templates.
input_range
; otherwise, returnsr without any casting.template<class F,class Tuple> constexprauto/*tuple-transform*/( F&& f, Tuple&& tuple) | (1) | (exposition only*) |
template<class F,class Tuple> constexprvoid/*tuple-for-each*/( F&& f, Tuple&& tuple) | (2) | (exposition only*) |
template<class T> constexpr T&/*as-lvalue*/( T&& t) | (3) | (exposition only*) |
Some range adaptors are specified in terms of these exposition-only function templates.
Following exposition-only concepts are used for several types, but they are not parts of the interface of standard library.
template<class R> concept/*simple-view*/= | (1) | (exposition only*) |
template<class I> concept/*has-arrow*/= | (2) | (exposition only*) |
template<class T,class U> concept/*different-from*/= | (3) | (exposition only*) |
template<class R> concept/*range-with-movable-references*/= | (4) | (exposition only*) |
template<bool C,class...Views> concept/*all-random-access*/= | (5) | (exposition only*) |
template<bool C,class...Views> concept/*all-bidirectional*/= | (6) | (exposition only*) |
template<bool C,class...Views> concept/*all-forward*/= | (7) | (exposition only*) |
#include <iostream>#include <ranges> int main(){autoconst ints={0,1,2,3,4,5};auto even=[](int i){return0== i%2;};auto square=[](int i){return i* i;}; // the "pipe" syntax of composing the views:for(int i: ints| std::views::filter(even)| std::views::transform(square))std::cout<< i<<' '; std::cout<<'\n'; // a traditional "functional" composing syntax:for(int i: std::views::transform(std::views::filter(ints, even), square))std::cout<< i<<' ';}
Output:
0 4 160 4 16
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3509 (P2281R1) | C++20 | it was unclear how range adaptor objects bound trailing arguments | they are bound by value |
LWG 3948 | C++23 | possibly-const-range andas-const-pointer were not declarednoexcept | declarednoexcept |
LWG 4027 | C++23 | possibly-const-range would not add const-qualificationfor ranges that has already modeled constant_range | adds const-qualification for such ranges |
LWG 4112 | C++20 | has-arrow did not requirei to be const-qualified | requires |