std::span rend() method
- od C++20
constexpr iterator rend() const noexcept;
Returns a reverse iterator
to the last element of the reversed view.It corresponds to the element preceding the first element of the non reversed view.
If the span is empty, the returned iterator will be equal to rbegin()
.
This method doesn't actually reverse the view, it just returns an iterator that points to the element before the first element of the view,
and which +
, -
, --
, ++
operators have slightly changed implementations.
For example it++
decrements the internal pointer and it--
increments it (so that traversing the container in a reverse order actually works).
Attempting to dereference a past the end iterator is undefined behaviour
.Parameters
(none)
Return value
Reverse iterator to the first element.
Complexity
Constant - O(1)..
Example
#include <algorithm>
#include <iostream>
#include <span>
#include <string_view>
void ascending(const std::span<const std::string_view> data,
const std::string_view term)
{
std::for_each(data.begin(), data.end(),
[](const std::string_view x) { std::cout << x << " "; });
std::cout << term;
}
void descending(const std::span<const std::string_view> data,
const std::string_view term)
{
std::for_each(data.rbegin(), data.rend(),
[](const std::string_view x) { std::cout << x << " "; });
std::cout << term;
}
int main()
{
constexpr std::string_view bars[]{ "▁","▂","▃","▄","▅","▆","▇","█" };
ascending(bars, " ");
descending(bars, "\n");
}
▁ ▂ ▃ ▄ ▅ ▆ ▇ █ █ ▇ ▆ ▅ ▄ ▃ ▂ ▁
Hover to see the original license.