std::multiset::rbegin,(3)

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

πŸ’… std::multiset::rbegin,(3) β€” C++ Standard Library

okay bestie so this is the page for std::multiset::rbegin πŸ’– and yes, the manual name is C++ Standard Library, we're in the big leagues now. it's all about walking your multiset backwards, main character energy only.


✨ NAME ✨

std::multiset::rbegin, β€” literally just std::multiset::rbegin,. no notes, that's the whole name, slay.


🌸 Synopsis 🌸

reverse_iterator rbegin();                       (1) (noexcept since C++11)
const_reverse_iterator rbegin() const;           (2) (noexcept since C++11)
const_reverse_iterator crbegin() const noexcept; (3) (since C++11)

so there's three of them, one per flavor 🫢 and no cap, overload (1) and (2) are noexcept since C++11, and (3) has been around since C++11 and is noexcept too.

what they do: returns a reverse iterator to the first element of the reversed multiset. iykyk that this corresponds to the last element of the non-reversed multiset. and if the multiset is empty? the returned iterator is equal to rend(). periodt. 🌈

there's a little diagram on the page called range-rbegin-rend.svg β€” it's giving "reverse iterator range visual," very demure, very helpful. πŸŽ€

πŸ¦‹ Parameters πŸ¦‹

(none)

nothing. zero. zip. ngl the least stressful parameter list ever πŸ’€


⭐ Return value ⭐

Reverse iterator to the first element. that's it, that's the whole vibe ✨


πŸ”₯ Complexity πŸ”₯

Constant. O(1) energy, we love a cheap call πŸ‘‘


πŸ’œ Notes πŸ’œ

  • Because both iterator and const_iterator are constant iterators (and may in fact be the same type), it is not possible to mutate the elements of the container through an iterator returned by any of these member functions. so don't even try, bestie 😭 the container said no.
  • The underlying iterator of the returned reverse iterator is the end iterator. Hence the returned iterator is invalidated if and when the end iterator is invalidated. ⚠️ keep that in mind, this is a real caveat, not a drill.
  • libc++ backports crbegin() to C++98 mode. cute of them honestly 🧚

🌊 Example 🌊

// Run this code

#include <iostream>
#include <set>

int main()
{
    std::multiset<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';
    }
}

it's a std::multiset<unsigned> called rep holding {1, 2, 3, 4, 1, 2, 3, 4} β€” so yeah, duplicates, it's a multiset, that's the whole point πŸ’… and then it walks backwards with crbegin() / crend() printing ⏼ n times per element. iconic.


πŸ“ Output πŸ“

⏼ ⏼ ⏼ ⏼
⏼ ⏼ ⏼ ⏼
⏼ ⏼ ⏼
⏼ ⏼ ⏼
⏼ ⏼
⏼ ⏼
⏼
⏼

look at that staircase 😭 it's literally descending and it's gorgeous.


🌈 See also 🌈

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

lowkey the whole takeaway: rbegin() gives you the last element as your "first," it's noexcept since C++11, it's constant time, and it will not let you mutate anything. you cannot change the elements. i said what i said. πŸ™ˆ

that's the page, bestie! πŸ«ΆπŸ’–βœ¨ this one came from http://cppreference.com, dated 2024.06.10, and it's std::multiset::rbegin,(3) β€” go forth and iterate backwards, you absolute legend πŸ‘‘πŸŽ€

πŸ“¦ where this page comes from