std::multimap::rbegin,(3)

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

πŸ’… std::multimap::rbegin,(3)

okay bestie, welcome to this page from the C++ Standard Libary manual β€” yes, that’s the manual name, and the title up there is std::multimap::rbegin,(3). This whole page is about the reverse-begin vibes of std::multimap. 🫢

✨ NAME ✨

std::multimap::rbegin, - std::multimap::rbegin,

no cap, that trailing comma is literally in the name and we are respecting it πŸ’€

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

Okay so this returns a reverse iterator to the first element of the reversed multimap. It corresponds to the last element of the non-reversed multimap. And if the multimap is empty, the returned iterator is equal to rend(). πŸŒ€

there’s also a diagram situation: range-rbegin-rend.svg β€” that’s the visual bestie for this exact range-rbegin-rend energy.

🫢 Parameters

(none)

🎁 Return value

Reverse iterator to the first element.

⚑ Complexity

Constant. periodt. ⚑

🧠 Notes

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.

Also, libc++ backports crbegin() to C++98 mode.

πŸ“ Example

// Run this code

#include <algorithm>
#include <iostream>
#include <string>
#include <map>

int main()
{
    std::multimap<std::string, int> map
    {
        {"β–ˆ", 1},
        {"β–’", 5},
        {"β–‘", 3},
        {"β–“", 7},
        {"β–“", 8},
        {"β–‘", 4},
        {"β–’", 6},
        {"β–ˆ", 2}
    };

    std::cout << "Print out in reverse order using const reverse iterators:\n";
    std::for_each(map.crbegin(), map.crend(),
        [](std::pair<const std::string, int> const& e)
        {
            std::cout << "{ \"" << e.first << "\", " << e.second << " };\n";
        });

    map.rbegin()->second = 42; // OK: non-const value is modifiable
//  map.crbegin()->second = 42; // Error: cannot modify the const value
}

🌈 Possible output

Print out in reverse order using const reverse iterators:
{ "β–“", 8 };
{ "β–“", 7 };
{ "β–’", 6 };
{ "β–’", 5 };
{ "β–‘", 4 };
{ "β–‘", 3 };
{ "β–ˆ", 2 };
{ "β–ˆ", 1 };

πŸ‘€ 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)

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::multimap::rbegin,(3) β€” mwah, go reverse-iterate responsibly πŸ’‹βœ¨

πŸ“¦ where this page comes from