| ||||||||||||||||||||||
| Range primitives | |||||||
| |||||||
| Range concepts | |||||||||||||||||||
| |||||||||||||||||||
| Range factories | |||||||||
| |||||||||
| Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper items | |||||||||||||||||
| |||||||||||||||||
Defined in header <ranges> | ||
template<ranges::view V> requiresranges::bidirectional_range<V> | (1) | (since C++20) |
namespace views{ inlineconstexpr/* unspecified */ reverse=/* unspecified */; | (2) | (since C++20) |
Call signature | ||
template<ranges::viewable_range R> requires/* see below */ | (since C++20) | |
view with reversed order.e is a (possibly cv-qualified) specialization ofreverse_view;I and valueK of typeranges::subrange_kind:K isranges::subrange_kind::sized;views::reverse unwraps reversed views if possible.Areverse_view always modelsbidirectional_range andcommon_range, and it modelsborrowed_range,sized_range, orrandom_access_range if the underlying view typeV models the corresponding concept.
Contents |
| Member | Description |
Vbase_(private) | the underlying view (exposition-only member object*) |
non-propagating-cache<ranges::iterator_t<V>>cached_end_(private)(present only if V does not satisfycommon_range) | an object that caches the result of calls tobegin()(exposition-only member object*) |
constructs areverse_view(public member function) | |
returns the underlying viewV(public member function) | |
returns the beginning iterator of thereverse_view(public member function) | |
returns the end iterator of thereverse_view(public member function) | |
| returns the size of the view if it is bounded (public member function) | |
(C++26) | returns the approximate size of the underlyingapproximately_sized_range(public member function) |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range(public member function of std::ranges::view_interface<D>)[edit] | |
(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface<D>)[edit] |
(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface<D>)[edit] |
| returns whether the derived view is not empty, provided only ifranges::empty is applicable to it (public member function of std::ranges::view_interface<D>)[edit] | |
returns the first element in the derived view, provided if it satisfiesforward_range(public member function of std::ranges::view_interface<D>)[edit] | |
returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range(public member function of std::ranges::view_interface<D>)[edit] | |
returns thenth element in the derived view, provided only if it satisfiesrandom_access_range(public member function of std::ranges::view_interface<D>)[edit] | |
reverse_view() requiresstd::default_initializable<V>=default; | (1) | (since C++20) |
constexpr reverse_view( V r); | (2) | (since C++20) |
| r | - | range to reverse |
constexpr V base()const& requiresstd::copy_constructible<V>; | (1) | (since C++20) |
constexpr V base()&&; | (2) | (since C++20) |
Returns the underlying view.
base_ ;.base_ );.constexprstd::reverse_iterator<ranges::iterator_t<V>> begin(); | (1) | (since C++20) |
constexprstd::reverse_iterator<ranges::iterator_t<V>> begin() requiresranges::common_range<V>; | (2) | (since C++20) |
constexprauto begin()const requiresranges::common_range<const V>; | (3) | (since C++20) |
base_ ), ranges::end(base_ ))).range concept, this function caches the result within the cache object for use on subsequent calls.base_ ));.constexprstd::reverse_iterator<ranges::iterator_t<V>> end(); | (1) | (since C++20) |
constexprauto end()const requiresranges::common_range<const V>; | (2) | (since C++20) |
Equivalent toreturnstd::make_reverse_iterator(ranges::begin(base_ ));.
constexprauto size() requiresranges::sized_range<V>; | (1) | (since C++20) |
constexprauto size()const requiresranges::sized_range<const V>; | (2) | (since C++20) |
Returns the size of the view if the view is bounded. Equivalent toreturnranges::size(base_ );.
constexprauto reserve_hint() requires ranges::approximately_sized_range<V>; | (1) | (since C++26) |
constexprauto reserve_hint()const requires ranges::approximately_sized_range<const V>; | (2) | (since C++26) |
Returnsranges::reserve_hint(base_ ).
template<class R> reverse_view( R&&)-> reverse_view<views::all_t<R>>; | (since C++20) | |
template<class T> constexprbool enable_borrowed_range<std::ranges::reverse_view<T>>= | (since C++20) | |
This specialization ofstd::ranges::enable_borrowed_range makesreverse_view satisfyborrowed_range when the underlying view satisfies it.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_ranges_reserve_hint | 202502L | (C++26) | ranges::approximately_sized_range andreserve_hint |
#include <iostream>#include <ranges> int main(){staticconstexprauto il={3,1,4,1,5,9}; std::ranges::reverse_view rv{il};for(int i: rv)std::cout<< i<<' ';std::cout<<'\n'; for(int i: il| std::views::reverse)std::cout<< i<<' ';std::cout<<'\n'; // operator[] is inherited from std::view_interfacefor(auto i{0U}; i!= rv.size();++i)std::cout<< rv[i]<<' ';std::cout<<'\n';}
Output:
9 5 1 4 1 39 5 1 4 1 39 5 1 4 1 3
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3494 | C++20 | reverse_view was never aborrowed_range | it is aborrowed_range if its underlying view is |
| iterator adaptor for reverse-order traversal (class template)[edit] | |
(C++20) | reverses the order of elements in a range (algorithm function object)[edit] |
(C++20) | creates a copy of a range that is reversed (algorithm function object)[edit] |