Przejdź do głównej zawartości

std::span rend() method

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().

zanotuj

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).

Undefined Behavior

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

Main.cpp
#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");
}
Output
▁ ▂ ▃ ▄ ▅ ▆ ▇ █  █ ▇ ▆ ▅ ▄ ▃ ▂ ▁
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::span rend() method

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().

zanotuj

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).

Undefined Behavior

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

Main.cpp
#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");
}
Output
▁ ▂ ▃ ▄ ▅ ▆ ▇ █  █ ▇ ▆ ▅ ▄ ▃ ▂ ▁
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.