title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
reverse_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> reverse_view class, which returns the elements of a range in reverse order. |
10/19/2022 |
|
|
|
A view of the elements of a range in reverse order.
template<ranges::view V>
requires ranges::bidirectional_range<V>
class reverse_view : public ranges::view_interface<reverse_view<V>>;
V
The type of the underlying view.
This type must satisfy ranges::bidirectional_range
.
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 |
Member functions | Description |
---|---|
ConstructorsC++20 | Construct a reverse_view . |
base C++20 |
Get the underlying range. |
begin C++20 |
Get an iterator to the first element. |
end C++20 |
Get the sentinel at the end of the reverse_view . |
size C++20 |
Get the number of elements. |
Inherited from view_interface |
Description |
back C++20 |
Get the last element. |
empty C++20 |
Test whether the reverse_view is empty. |
front C++20 |
Get the first element. |
operator[] C++20 |
Get the element at the specified position. |
operator bool C++20 |
Test whether the reverse_view isn't empty. |
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
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)
rg
The view to provide a reversed view of.
For information about the template parameter type, see Template parameters.
A view of the underlying range, in reverse order.
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.
// 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
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() &&;
None.
The underlying view.
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>;
None.
An iterator pointing at the first element in the reverse_view
.
:::image type="content" source="media/begin-end-sentinel.png" alt-text="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().":::
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
.
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>;
None.
The sentinel that follows the last element in the reverse_view
.
:::image type="content" source="media/begin-end-sentinel.png" alt-text="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().":::
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.
Get the number of elements.
constexpr auto size() requires ranges::sized_range<V>;
constexpr auto size() const requires ranges::sized_range<const V>;
None.
The number of elements in the reverse_view
.
The size of the view is only available if the underlying range is a sized_range
, or in other words, bounded.