Namespaces
Variants
Views
Actions

Ranges library (C++20)

From natekohl.net
< cpp
 
 
 

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]
(C++20)
returns an iterator to the end of a range
(customization point object) [edit]
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)
obtains the size of a range whose size can be calculated in constant time
(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]
Range primitives
obtains associated 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 range which also models __ForwardingRange
(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 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 views, using the curiously recurring template pattern
(class template) [edit]
combines an iterator-sentinel pair into a view
(class template) [edit]

[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]

[edit] Range adaptors

Range adaptors accept viewable_range as their first arguments and returns a view. They can also be chained using the pipe operator: if C and D are a range adaptors and R is a viewable_range, these two expressions are equivalent:

D(C(R))
R | C | D

If an adaptor takes multiple arguments, these forms are equivalent:

adaptor(range, args...)
adaptor(args...)(range)
range | adaptor(args...)
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 elmeents
(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) [edit]
takes a view consisting of tuple-like values and a number N and produces a view of N'th 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.

[edit] 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>>;

[edit] 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