Ranges library (C++20)

From cppreference.com
< 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 view = ranges::view;

}

The namespace alias std::view is provided as a shorthand for std::ranges::view.


Defined in header <ranges>
Defined in namespace std::ranges
Range access
returns an iterator to the beginning of a range
(customization point object)
returns an iterator to the end of a range
(customization point object)
returns a reverse iterator to a range
(customization point object)
returns a reverse end iterator to a range
(customization point object)
obtains the size of a range whose size can be calculated in constant time
(customization point object)
checks whether a range is empty
(customization point object)
obtains a pointer to the beginning of a contiguous range
(customization point object)
Range primitives
obtains the iterator and sentinel types of a range
(alias template)
Dangling iterator handling
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 Range which also models __ForwardingRange
(alias template)
Range concepts
specifies that a type is a range, that is, it provides a begin iterator and an end sentinel
(concept)
specifies that a range knows its size in constant time
(concept)
specifies that a range is a view, that is, it has constant time copy/move/assignment
(concept)
specifies a range whose iterator type satisfies InputIterator
(concept)
specifies a range whose iterator type satisfies OutputIterator
(concept)
specifies a range whose iterator type satisfies ForwardIterator
(concept)
specifies a range whose iterator type satisfies BidirectionalIterator
(concept)
specifies a range whose iterator type satisfies RandomAccessIterator
(concept)
specifies a range whose iterator type satisfies ContiguousIterator
(concept)
specifies that a range has identical iterator and sentinel types
(concept)
specifies the requirements for a Range to be safely convertible to a View
(concept)
Views
helper class template for defining Views, using the curiously recurring template pattern
(class template)
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)
a View consisting of a sequence generated by repeatedly incrementing an initial value
(class template) (customization point object)
creates a subrange from an iterator and a count
(customization point object)

Range adaptors

Range adaptors accept ViewableRange 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 ViewableRange, 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)
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)
a View consisting of the first N elements of another View
(class template) (range adaptor object)
a View consisting of the sequence obtained from flattening a View of Ranges
(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 CommonRange
(class template) (range adaptor object)
a View that iterates over the elements of another bidirectional view in reverse order
(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
    View<R> && Range<const R> &&
    Same<std::ranges::iterator_t<R>, std::ranges::iterator_t<const R>> &&

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

  concept __NotSameAs =                          // exposition only

    !Same<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::view::filter(even) | std::view::transform(square)) {
    std::cout << i << ' ';
  }
}

Output:

0 4 16