Ranges library (C++20)
The ranges library provides components for dealing with ranges of elements, including a variety of view adapters.
Defined in header <ranges>
|
||
namespace std { namespace views = ranges::views; |
||
The namespace alias std::views
is provided as a shorthand for std::ranges::views
.
Defined in header
<ranges> | ||
Defined in namespace
std::ranges | ||
Range access | ||
(C++20) |
returns an iterator to the beginning of a range (customization point object) | |
(C++20) |
returns a sentinel indicating the end of a range (customization point object) | |
(C++20) |
returns a reverse iterator to a range (customization point object) | |
(C++20) |
returns a reverse end iterator to a range (customization point object) | |
(C++20) |
returns an integer equal to the size of a range (customization point object) | |
(C++20) |
checks whether a range is empty (customization point object) | |
(C++20) |
obtains a pointer to the beginning of a contiguous range (customization point object) | |
Range primitives | ||
(C++20)(C++23)(C++20)(C++23) |
obtains iterator and sentinel types of a range (alias template) | |
Dangling iterator handling | ||
(C++20) |
a placeholder type indicating that an iterator or a subrange should not be returned since it would be dangling (class) | |
obtains iterator type or subrange type of a borrowed_range (alias template) | ||
Range concepts | ||
(C++20) |
specifies that a type is a range, that is, it provides a begin iterator and an end sentinel (concept) | |
(C++20) |
specifies that a type is a range and iterators obtained from an expression of it can be safely returned without danger of dangling (concept) | |
(C++20) |
specifies that a range knows its size in constant time (concept) | |
(C++20) |
specifies that a range is a view, that is, it has constant time copy/move/assignment (concept) | |
(C++20) |
specifies a range whose iterator type satisfies input_iterator (concept) | |
(C++20) |
specifies a range whose iterator type satisfies output_iterator (concept) | |
(C++20) |
specifies a range whose iterator type satisfies forward_iterator (concept) | |
(C++20) |
specifies a range whose iterator type satisfies bidirectional_iterator (concept) | |
(C++20) |
specifies a range whose iterator type satisfies random_access_iterator (concept) | |
(C++20) |
specifies a range whose iterator type satisfies contiguous_iterator (concept) | |
(C++20) |
specifies that a range has identical iterator and sentinel types (concept) | |
(C++20) |
specifies the requirements for a range to be safely convertible to a view (concept) | |
Views | ||
(C++20) |
helper class template for defining a view , using the curiously recurring template pattern (class template) | |
(C++20) |
combines an iterator-sentinel pair into a view (class template) |
Range factories
Defined in header
<ranges> | |
Defined in namespace
std::ranges | |
an empty view with no elements(class template) (variable template) | |
a view that contains a single element of a specified value(class template) (customization point object) | |
(C++20) |
a view consisting of a sequence generated by repeatedly incrementing an initial value(class template) (customization point object) |
(C++20) |
creates a subrange from an iterator and a count (customization point object) |
Range adaptors
Range adaptors accept viewable_range
as their first arguments and return a view
.
If an adaptor takes only one argument, it can also be called using the pipe operator: if C is a range adaptor object and R is a viewable_range
, these two expressions are equivalent:
C(R) R | C
Unary range adaptors can also be chained to produce another range adaptor: if C and D are range adaptor objects and R is a viewable_range
, then C | D is also a range adaptor object, and these two expressions are equivalent:
R | C | D // (R | C) | D R | (C | D)
If an adaptor takes multiple arguments, these forms are equivalent:
adaptor(range, args...) adaptor(args...)(range) range | adaptor(args...)
In this case, adaptor(args...) is a unary range adaptor object.
Defined in header
<ranges> | |
Defined in namespace
std::ranges | |
(C++20) |
a view that includes all elements of a range (alias template) (range adaptor object) |
(C++20) |
a view of the elements of some other range (class template) |
a view that consists of the elements of a range that satisfies a predicate(class template) (range adaptor object) | |
a view of a sequence that applies a transformation function to each element(class template) (range adaptor object) | |
(C++20) |
a view consisting of the first N elements of another view (class template) (range adaptor object) |
a view consisting of the initial elements of another view , until the first element on which a predicate returns false(class template) (range adaptor object) | |
(C++20) |
a view consisting of elements of another view , skipping the first N elements(class template) (range adaptor object) |
a view consisting of the elements of another view , skipping the initial subsequence of elements until the first element where the predicate returns false(class template) (range adaptor object) | |
(C++20) |
a view consisting of the sequence obtained from flattening a view of range s(class template) (range adaptor object) |
a view over the subranges obtained from splitting another view using a delimiter(class template) (range adaptor object) | |
converts a view into a common_range (class template) (range adaptor object) | |
a view that iterates over the elements of another bidirectional view in reverse order(class template) (range adaptor object) | |
a view consisting of the elements obtained by successive application of operator>> on the associated input stream(class template) (customization point object) | |
takes a view consisting of tuple-like values and a number N and produces a view of Nth element of each tuple(class template) (range adaptor object) | |
(C++20) |
takes a view consisting of pair-like values and produces a view of the first elements of each pair(class template) (range adaptor object) |
takes a view consisting of pair-like values and produces a view of the second elements of each pair(class template) (range adaptor object) |
Some range adaptors wrap their element or function object with the semiregular wrapper.
Helper concepts
Following exposition-only concepts are used for several types, but they are not parts of the interface of standard library.
template<class R> concept __SimpleView = // exposition only |
||
template<class T, class U> concept __NotSameAs = // exposition only |
||
Example
#include <vector> #include <ranges> #include <iostream> int main() { std::vector<int> ints{0,1,2,3,4,5}; auto even = [](int i){ return 0 == i % 2; }; auto square = [](int i) { return i * i; }; for (int i : ints | std::views::filter(even) | std::views::transform(square)) { std::cout << i << ' '; } }
Output:
0 4 16