Namespaces
Variants
Views
Actions

Ranges library (C++20)

From cppreference.com
< cpp
Revision as of 01:28, 27 April 2020 by Levovix (Talk | contribs)

 
 
Ranges library
Range adaptors
 

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

Contents

Range access
returns an iterator to the beginning of a range
(customization point object)[edit]
returns a sentinel indicating the end of a range
(customization point object)[edit]
returns a reverse iterator to a range
(customization point object)[edit]
returns a reverse end iterator to a range
(customization point object)[edit]
returns an integer equal to the size of a range
(customization point object)[edit]
checks whether a range is empty
(customization point object)[edit]
obtains a pointer to the beginning of a contiguous range
(customization point object)[edit]
Range primitives
obtains iterator and sentinel types of a range
(alias template)[edit]
Dangling iterator handling
a placeholder type indicating that an iterator or a subrange should not be returned since it would be dangling
(class) [edit]
obtains iterator type or subrange type of a borrowed_range
(alias template)[edit]
Range concepts
specifies that a type is a range, that is, it provides a begin iterator and an end sentinel
(concept) [edit]
specifies that a type is a range and iterators obtained from an expression of it can be safely returned without danger of dangling
(concept) [edit]
specifies that a range knows its size in constant time
(concept) [edit]
specifies that a range is a view, that is, it has constant time copy/move/assignment
(concept) [edit]
specifies a range whose iterator type satisfies input_iterator
(concept) [edit]
specifies a range whose iterator type satisfies output_iterator
(concept) [edit]
specifies a range whose iterator type satisfies forward_iterator
(concept) [edit]
specifies a range whose iterator type satisfies bidirectional_iterator
(concept) [edit]
specifies a range whose iterator type satisfies random_access_iterator
(concept) [edit]
specifies a range whose iterator type satisfies contiguous_iterator
(concept) [edit]
specifies that a range has identical iterator and sentinel types
(concept) [edit]
specifies the requirements for a range to be safely convertible to a view
(concept) [edit]
Views
helper class template for defining a view, using the curiously recurring template pattern
(class template) [edit]
combines an iterator-sentinel pair into a view
(class template) [edit]

Range factories

Defined in header <ranges>
Defined in namespace std::ranges
an empty view with no elements
(class template) (variable template)[edit]
a view that contains a single element of a specified value
(class template) (customization point object)[edit]
a view consisting of a sequence generated by repeatedly incrementing an initial value
(class template) (customization point object)[edit]
creates a subrange from an iterator and a count
(customization point object)[edit]

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
a view that includes all elements of a range
(alias template) (range adaptor object)[edit]
a view of the elements of some other range
(class template) [edit]
a view that consists of the elements of a range that satisfies a predicate
(class template) (range adaptor object)[edit]
a view of a sequence that applies a transformation function to each element
(class template) (range adaptor object)[edit]
a view consisting of the first N elements of another view
(class template) (range adaptor object)[edit]
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)[edit]
a view consisting of elements of another view, skipping the first N elements
(class template) (range adaptor object)[edit]
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)[edit]
a view consisting of the sequence obtained from flattening a view of ranges
(class template) (range adaptor object)[edit]
a view over the subranges obtained from splitting another view using a delimiter
(class template) (range adaptor object)[edit]
converts a view into a common_range
(class template) (range adaptor object)[edit]
a view that iterates over the elements of another bidirectional view in reverse order
(class template) (range adaptor object)[edit]
a view consisting of the elements obtained by successive application of operator>> on the associated input stream
(class template) (customization point object)[edit]
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)[edit]
takes a view consisting of pair-like values and produces a view of the first elements of each pair
(class template) (range adaptor object)[edit]
takes a view consisting of pair-like values and produces a view of the second elements of each pair
(class template) (range adaptor object)[edit]

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
    ranges::view<R> && ranges::range<const R> &&
    std::same_as<std::ranges::iterator_t<R>, std::ranges::iterator_t<const R>> &&

    std::same_as<std::ranges::sentinel_t<R>, std::ranges::sentinel_t<const R>>;
template<class T, class U>

  concept __NotSameAs =                          // exposition only

    !std::same_as<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;

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