From cppreference.com
Defined in header
template<ranges::viewV>requiresranges::bidirectional_range<V>classreverse_view:publicranges::view_interface<reverse_view<V>> (1) (since C++20)namespaceviews{inlineconstexpr/* unspecified */reverse=/* unspecified */;} (2) (since C++20)Call signature
template<ranges::viewable_rangeR>requires/* see below */constexprranges::viewautoreverse(R&&r);(since C++20)1) A range adaptor that represents a view of underlying
with reversed order.
2)
. The expression views::reverse(e) is
to one of the following expressions, except that e is evaluated only once:
e.base(), if the type of e is a (possibly cv-qualified) specialization of reverse_view;
otherwise, if the type of e is (possibly cv-qualified) ranges::subrange<std::reverse_iterator<I>,std::reverse_iterator<I>,K> for some iterator type I and value K of type ranges::subrange_kind:
ranges::subrange<I,I,K>(e.end().base(),e.begin().base(),e.size()), if K is ranges::subrange_kind::sized;
otherwise ranges::subrange<I,I,K>(e.end().base(),e.begin().base());
otherwise ranges::reverse_view{e}.
In other words, views::reverse unwraps reversed views if possible.
A reverse_view always models
and
, and it models
,
, or
if the underlying view type V models the corresponding concept.
Data members
Member Description Vbase_(private)the underlying view
(exposition-only member object*)
<ranges::iterator_t<V>>cached_end_(private)
(present only if V does not satisfy
)an object that caches the result of calls to
(exposition-only member object*)Member functions
constructs a reverse_view
(public member function)
returns the underlying view V
(public member function)
returns the beginning iterator of the reverse_view
(public member function)
returns the end iterator of the reverse_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 underlying
(public member function) Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
std::ranges::reverse_view::reverse_view
reverse_view()requiresstd::default_initializable<V>=default; (1) (since C++20)constexprreverse_view(Vr); (2) (since C++20)1) Value-initializes
via its default member initializer (=V()).
2) Initializes
with std::move(r).
Parameters
r - range to reverse std::ranges::reverse_view::base
constexprVbase()const&requiresstd::copy_constructible<V>; (1) (since C++20)constexprVbase()&&; (2) (since C++20)Returns the underlying view.
1) Copy-constructs the result from the underlying view. Equivalent to return
;.
2) Move-constructs the result from the underlying view. Equivalent to returnstd::move(
);.
std::ranges::reverse_view::begin
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)constexprautobegin()constrequiresranges::common_range<constV>; (3) (since C++20)1) Returns std::make_reverse_iterator(ranges::next(ranges::begin(
),
ranges::end(
))).
In order to provide the amortized constant time complexity required by the
concept, this function caches the result within the cache object for use on subsequent calls.
2,3) Equivalent to returnstd::make_reverse_iterator(ranges::end(
));.
std::ranges::reverse_view::end
constexprstd::reverse_iterator<ranges::iterator_t<V>>end(); (1) (since C++20)constexprautoend()constrequiresranges::common_range<constV>; (2) (since C++20)Equivalent to returnstd::make_reverse_iterator(ranges::begin(
));.
std::ranges::reverse_view::size
constexprautosize()requiresranges::sized_range<V>; (1) (since C++20)constexprautosize()constrequiresranges::sized_range<constV>; (2) (since C++20)Returns the size of the view if the view is bounded. Equivalent to returnranges::size(
);.
std::ranges::as_rvalue_view::reserve_hint
constexprautoreserve_hint()requiresranges::approximately_sized_range<V>; (1) (since C++26)constexprautoreserve_hint()constrequiresranges::approximately_sized_range<constV>; (2) (since C++26)Returns ranges::reserve_hint(
).
Deduction guides
template<classR>reverse_view(R&&)->reverse_view<views::all_t<R>>;(since C++20)Helper templates
template<classT>constexprboolenable_borrowed_range<std::ranges::reverse_view<T>>=ranges::enable_borrowed_range<T>;(since C++20)This specialization of
std::ranges::enable_borrowed_range
makes reverse_view satisfy
when the underlying view satisfies it.
Notes
macroValueStdFeature
(C++26)ranges::approximately_sized_range and
Example
Run this code
#include<iostream>#include<ranges>intmain(){staticconstexprautoil={3,1,4,1,5,9};std::ranges::reverse_viewrv{il};for(inti:rv)std::cout<<i<<' ';std::cout<<'\n';for(inti:il|std::views::reverse)std::cout<<i<<' ';std::cout<<'\n';// operator[] is inherited from std::view_interfacefor(autoi{0U};i!=rv.size();++i)std::cout<<rv[i]<<' ';std::cout<<'\n';}Output:
9 5 1 4 1 3 9 5 1 4 1 3 9 5 1 4 1 3 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++20 reverse_view was never a borrowed_rangeit is a borrowed_range if its underlying view is See also