# std::ranges::view::reverse, std::ranges::reverse_view

From cppreference.com
< cpp‎ | ranges

template< View V > requires BidirectionalRange <V> class reverse_view : public ranges::view_interface<reverse_view<V>> | (1) | (since C++20)
namespace view { 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 view::reverse(E) is expression-equivalent to (has the same effect as) 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, `view::reverse` unwraps reversed views if possible.

### Data members

## std::ranges::reverse_view::base_

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

the underlying view

### Member functions

## std::ranges::reverse_view::reverse_view

reverse_view() = default; | (1) |
constexpr reverse_view(V base); | (2) |
template<ViewableRange R> requires BidirectionalRange <R> && Constructible <V, all_view<R>> constexpr reverse_view(R&& r); | (3) |

1) Value-initializes `base_`
2) Initializes `base_` with std::move(base).
3) Initializes `base_` with view::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 CommonRange<V>; | (2) |
constexpr std::reverse_iterator <ranges::iterator_t<const V>> begin() const requires CommonRange<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 CommonRange<V>; |  |
constexpr std::reverse_iterator <ranges::iterator_t<const V>> end() const requires CommonRange<const V>; |  |

Equivalent to return std::make_reverse_iterator(ranges::begin(base_));.

## std::ranges::reverse_view::size

constexpr auto size() requires SizedRange <V> { return ranges::size(base_); } |  |
constexpr auto size() const requires SizedRange <const V> { return ranges::size(base_); } |  |

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

### Deduction guides

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

### Example

Run this code

    #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

### See also

reverse_iterator | iterator adaptor for reverse-order traversal (class template)

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/reverse_view&oldid=110779"