Namespaces
Variants
Views
Actions

std::ranges::views::reverse, std::ranges::reverse_view

From cppreference.com
< cpp‎ | ranges
 
 
 
template< ranges::view V >

    requires ranges::bidirectional_range<V>

class reverse_view : public ranges::view_interface<reverse_view<V>>
(1) (since C++20)
namespace views {

    inline constexpr /*unspecified*/ reverse = /*unspecified*/;

}
(2) (since C++20)
1) A range adaptor that represents a view of underlying view with reversed order.
2) The expression views::reverse(E) is expression-equivalent 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.

Contents

[edit] Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

[edit] Data members

std::ranges::reverse_view::base_

V base_ = V(); /* exposition-only */

the underlying view

[edit] Member functions

std::ranges::reverse_view::reverse_view

reverse_view() = default;
(1)
constexpr reverse_view(V base);
(2)
template<ranges::viewable_range R>

    requires
        ranges::bidirectional_range<R> &&
        std::constructible_from<V, ranges::all_view<R>>

constexpr reverse_view(R&& r);
(3)
1) Value-initializes base_
2) Initializes base_ with std::move(base).
3) Initializes base_ with views::all(std::forward<R>(r)).

Parameters

r - range to reverse

std::ranges::reverse_view::base

constexpr V base() const;

Equivalent to return base_;

std::ranges::reverse_view::begin

constexpr std::reverse_iterator<ranges::iterator_t<V>> begin();
(1)
constexpr std::reverse_iterator<ranges::iterator_t<V>> begin()
  requires ranges::common_range<V>;
(2)
constexpr std::reverse_iterator<ranges::iterator_t<const V>> begin() const
  requires ranges::common_range<const V>;
(2)
1) Returns std::make_reverse_iterator(ranges::next(ranges::begin(base_), ranges::end(base_))). In order to provide the amortized constant time complexity required by the range concept, this function caches the result within the reverse_view object for use on subsequent calls.
2) Equivalent to return std::make_­reverse_­iterator(ranges::end(base_­));.

std::ranges::reverse_view::end

constexpr std::reverse_iterator<ranges::iterator_t<V>> end()
  requires ranges::common_range<V>;
constexpr std::reverse_iterator<ranges::iterator_t<const V>> end() const
  requires ranges::common_range<const V>;

Equivalent to return std::make_­reverse_­iterator(ranges::begin(base_­));.

std::ranges::reverse_view::size

constexpr auto size() requires ranges::sized_range<V> {

    return ranges::size(base_);

}
constexpr auto size() const requires ranges::sized_range<const V> {

    return ranges::size(base_);

}

Returns the size of the view if the view is bounded.

[edit] Deduction guides

template<class R>
reverse_view(R&&) -> reverse_view<ranges::all_view<R>>;

[edit] Example

#include <array>
#include <ranges>
#include <iostream>
 
int main()
{
    std::array<int, 6> il {3, 1, 4, 1, 5, 9};
    std::ranges::reverse_view rv {il};
    for (int i : rv)
        std::cout << i << ' ';
}

Output:

9 5 1 4 1 3

[edit] See also

iterator adaptor for reverse-order traversal
(class template) [edit]