# Ranges library (C++20)

< cpp

C++
 Language Standard Library Headers Freestanding and hosted implementations Named requirements Language support library Concepts library (C++20) Diagnostics library Utilities library Strings library Containers library Iterators library Ranges library (C++20) Algorithms library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications

Ranges library
Range access
Range primitives
Dangling iterator handling
Range concepts
Views
Semiregular wrapper
Factories
 ref_view split_viewviews::split take_while_viewviews::take_while elements_viewviews::elements

The ranges library provides components for dealing with ranges of elements, including a variety of view adapters.

 Defined in header  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) 
(C++20)
returns an iterator to the end of a range
(customization point object) 
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)
obtains the size of a range whose size can be calculated in constant time
(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
obtains associated 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 safe_range
(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 type is a range and iterators obtained from an expression of it can be safely returned without danger of dangling
(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 input_iterator
(concept) 
specifies a range whose iterator type satisfies output_iterator
(concept) 
specifies a range whose iterator type satisfies forward_iterator
(concept) 
specifies a range whose iterator type satisfies bidirectional_iterator
(concept) 
specifies a range whose iterator type satisfies random_access_iterator
(concept) 
specifies a range whose iterator type satisfies contiguous_iterator
(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  Defined in namespace std::ranges empty_viewviews::empty an empty view with no elements (class template) (variable template)  single_viewviews::single a view that contains a single element of a specified value (class template) (customization point object)  iota_viewviews::iota a view consisting of a sequence generated by repeatedly incrementing an initial value (class template) (customization point object)  views::counted 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...)
range | adaptor(args...)

In this case, adaptor(args...) is a unary range adaptor object.

 Defined in header  Defined in namespace std::ranges all_viewviews::all a view that includes all elements of a range (alias template) (range adaptor object)  ref_view a view of the elements of some other range (class template)  filter_viewviews::filter a view that consists of the elements of a range that satisfies a predicate (class template) (range adaptor object)  transform_viewviews::transform a view of a sequence that applies a transformation function to each element (class template) (range adaptor object)  take_viewviews::take a view consisting of the first N elements of another view (class template) (range adaptor object)  take_while_viewviews::take_while 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)  drop_viewviews::drop a view consisting of elements of another view, skipping the first N elements (class template) (range adaptor object)  drop_while_viewviews::drop_while 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)  join_viewviews::join a view consisting of the sequence obtained from flattening a view of ranges (class template) (range adaptor object)  split_viewviews::split a view over the subranges obtained from splitting another view using a delimiter (class template) (range adaptor object)  common_viewviews::common converts a view into a common_range (class template) (range adaptor object)  reverse_viewviews::reverse a view that iterates over the elements of another bidirectional view in reverse order (class template) (range adaptor object)  basic_istream_viewistream_view a view consisting of the elements obtained by successive application of operator>> on the associated input stream (class template)  elements_viewviews::elements 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)  keys_viewviews::keys takes a view consisting of pair-like values and produces a view of the first elements of each pair (class template) (range adaptor object)  values_viewviews::values 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   concept __SimpleView =                         // exposition only     ranges::view && ranges::range &&     std::same_as, std::ranges::iterator_t> &&     std::same_as, std::ranges::sentinel_t>; template   concept __NotSameAs =                          // exposition only     !std::same_as, std::remove_cvref_t>;

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