Namespaces
Variants
Views
Actions

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

Contents

Range access
returns an iterator to the beginning of a range
(customization point object) [edit]
returns an iterator to 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]
obtains the size of a range whose size can be calculated in constant time
(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 the 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 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 InputIterator
(concept) [edit]
specifies a range whose iterator type satisfies OutputIterator
(concept) [edit]
specifies a range whose iterator type satisfies ForwardIterator
(concept) [edit]
specifies a range whose iterator type satisfies BidirectionalIterator
(concept) [edit]
specifies a range whose iterator type satisfies RandomAccessIterator
(concept) [edit]
specifies a range whose iterator type satisfies ContiguousIterator
(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 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) [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 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 CommonRange
(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]

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
    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>>;

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

Output:

0 4 16