std::flat_set::rend,(3)

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

πŸ’… NAME

std::flat_set::rend β€” okay so this is literally the rend of std::flat_set, bestie. It's the reverse-end iterator situation, and yes it's giving "the end but backwards" ✨

Also hi, this page lives in the C++ Standard Library section of the manual (that's the manual name, folding it in near the top like you asked 🫢).


πŸŽ€ Synopsis

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

So what does it actually do? It returns a reverse iterator pointing to the element following the last element of the reversed flat_set. Which sounds like nonsense until you remember reverse iterators are little gremlins: it corresponds to the element preceding the first element of the non-reversed flat_set (deranged, iconic, no notes πŸ’…).

That element is just a placeholder β€” it's not a real thing you can look at. Attempting to access it results in undefined behavior 😭 so like… don't.

range-rbegin-rend.svg β€” yeah that's the little diagram that ships with this page, love that for us πŸ¦‹


🌸 Parameters

(none) β€” zero, nada, zip. She takes no arguments and she's thriving πŸ’–


✨ Return value

A reverse iterator to the element following the last element. That's it, that's the whole return value πŸ‘‘


⭐ Complexity

Constant. O(1), instant, no cap πŸ”₯


πŸ’œ Notes

Okay important detail, bestie: both iterator and const_iterator are constant iterators here (and they may in fact be the same type, which is a whole vibe). So it is not possible to mutate the elements of the container through an iterator returned by any of these member functions. No sneaky writes. Not today. πŸ™ˆ


🌈 Example

// Run this code

#include <iostream>
#include <flat_set>

int main()
{
    std::flat_set<unsigned> rep{1, 2, 3, 4, 1, 2, 3, 4};

    for (auto it = rep.crbegin(); it != rep.crend(); ++it)
    {
        for (auto n = *it; n > 0; --n)
            std::cout << "⏼" << ' ';
        std::cout << '\n';
    }
}

πŸ“ Output

⏼ ⏼ ⏼ ⏼
⏼ ⏼ ⏼
⏼ ⏼
⏼

Look at that little staircase, we ate 🧚


πŸ‘‘ See also

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

fr fr that's the whole page β€” go forth and iterate backwards responsibly πŸ’–

Source/version and date, cute sign-off style: this one came from http://cppreference.com, dated 2024.06.10, and the page itself is std::flat_set::rend,(3). Periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from