reverse_view class (C++ Standard Library)

A view of the elements of a range in reverse order.

Syntax

template<ranges::view V>
requires ranges::bidirectional_range<V>
class reverse_view : public ranges::view_interface<reverse_view<V>>;

Template parameters

V
The type of the underlying view.
This type must satisfy ranges::bidirectional_range.

View characteristics

For a description of the following entries, see View class characteristics

Characteristic Description
Range adaptor views::reverse
Underlying range Must satisfy bidirectional_range up to random_access_range
Element type Same as the underlying range
View iterator category Same as the underlying range
Sized Only if the underlying range satisfies sized_range
Is const-iterable Only if the underlying range is a common_view and satisfies const-iterable
Common range Yes
Borrowed range Only if the underlying range satisfies borrowed_range

Members

Member functions Description
ConstructorsC++20 Construct a reverse_view.
baseC++20 Get the underlying range.
beginC++20 Get an iterator to the first element.
endC++20 Get the sentinel at the end of the reverse_view.
sizeC++20 Get the number of elements.
Inherited from view_interface Description
backC++20 Get the last element.
emptyC++20 Test whether the reverse_view is empty.
frontC++20 Get the first element.
operator[]C++20 Get the element at the specified position.
operator boolC++20 Test whether the reverse_view isn't empty.

Requirements

Header: <ranges> (since C++20)

Namespace: std::ranges

Compiler Option: /std:c++20 or later is required.

Constructors

Construct an instance of a reverse_view from a bidirectional view.

1) reverse_view() requires default_initializable<V> = default; // default-constructs the underlying view
2) constexpr explicit reverse_view(V rg); // initializes the underlying view via std::move(r)

Parameters

rg
The view to provide a reversed view of.

For information about the template parameter type, see Template parameters.

Return value

A view of the underlying range, in reverse order.

Remarks

The best way to create a reverse_view is by using the views::reverse range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.

1) The default constructor default-initializes a reverse_view.
2) Create a reverse_view from the specified view.

Example: reverse_view

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{0, 1, 2, 3, -4, 5, 6};
    auto rv = v | std::views::reverse;
    
    for (auto e : rv) // 6 5 -4 3 2 1 0
    {
        std::cout << e << ' ';
    }
}
6 5 -4 3 2 1 0

base

Gets a copy of the underlying view.

// Uses a copy constructor to return the underlying view
constexpr V base() const& requires std::copy_constructible<V>;

// Uses a move constructor to return the underlying view
constexpr V base() &&;

Parameters

None.

Return value

The underlying view.

begin

Get an iterator to the first element in the reverse_view.

1) constexpr reverse_iterator<iterator_t<V>> begin();
2) constexpr reverse_iterator<iterator_t<V>> begin() requires common_range<V>;
3) constexpr auto begin() const requires common_range<const V>;

Parameters

None.

Return value

An iterator pointing at the first element in the reverse_view.

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

Remarks

After the first call to begin(), subsequent calls run in constant time, O(1), no matter how many elements are in the reverse_view. This has implications because reverse_view caches the value of last so it can return it repeatedly from begin. This means you shouldn't reuse a view after the underlying container is modified. If the underlying range is modified, generate a new view, which is inexpensive.

2) The underlying view must satisfy common_range, which means that the underlying view must have the same begin and end iterator type.
3) The underlying view must satisfy common_range for a const view to iterate over a const reverse_view.

end

Get the sentinel at the end of the reverse_view

1) constexpr reverse_iterator<iterator_t<V>> end();
2) constexpr auto end() const requires common_range<const V>;

Parameters

None.

Return value

The sentinel that follows the last element in the reverse_view.

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

Remarks

For 2, the underlying view must satisfy common_range for a const view, which means that the underlying view must have the same begin and end iterator type.

size

Get the number of elements.

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

Parameters

None.

Return value

The number of elements in the reverse_view.

Remarks

The size of the view is only available if the underlying range is a sized_range, or in other words, bounded.

See also

<ranges>
reverse range adaptor
View classes