std::span::...span::crend(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ std::span::rend, std::span::crend β€” C++ Standard Libary

okay bestie, welcome to the std::span::...span::crend(3) page from the C++ Standard Libary. this is the reverse-iterator end-boss squad, and it is lowkey iconic. πŸ’…


🌸 NAME 🌸

std::span::rend,std::span::crend β€” std::span::rend,std::span::crend


✨ Synopsis ✨

constexpr reverse_iterator rend() const noexcept;        (1) (since C++20)
constexpr const_reverse_iterator crend() const noexcept; (2) (since C++23)

So here's the vibe: these functions return a reverse iterator to the element following the last element of the reversed span. It corresponds to the element preceding the first element of the non-reversed span. That element is literally just a placeholder, and attempting to access it results in undefined behavior. not cute, bestie. 😭

The original docs also drop a diagram called range-rbegin-rend.svg, which is very demure, very cppreference. πŸ¦‹


πŸ“ Parameters πŸ“

(none)


πŸ’– Return value πŸ’–

Reverse iterator to the element following the last element. periodt. ✨


⭐ Complexity ⭐

Constant.


🧚 Example 🧚

// Run this code

#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:

▁ β–‚ β–ƒ β–„ β–… β–† β–‡ β–ˆ  β–ˆ β–‡ β–† β–… β–„ β–ƒ β–‚ ▁

🌈 See also 🌈

  • rbegin β€” returns a reverse iterator to the beginning
  • crbegin β€” (public member function) (C++23)
  • rend β€” returns a reverse end iterator for a container or array
  • crend β€” (function template) (C++14)

mwah πŸ’‹ that's the whole page. source: http://cppreference.com, version/date: 2024.06.10, manual page: std::span::...span::crend(3). stay safe, and remember: that placeholder is for looking, not touching β€” undefined behavior is not the moment. πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from