std::flat_map::rbegin,(3)

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

πŸŽ€ std::flat_map::rbegin,(3) πŸŽ€

Okay bestie, this page is from the C++ Standard Libary manual, and it's all about std::flat_map::rbegin, β€” reverse iterator energy for std::flat_map, plus the const twin crbegin(). Let's go πŸ’…


πŸ’– NAME πŸ’–

std::flat_map::rbegin, β€” that's the name, comma and all. The original NAME line literally reads std::flat_map::rbegin, - std::flat_map::rbegin, ✨

🌸 Synopsis 🌸

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

Three overloads, all noexcept, all since C++23: regular rbegin() is (1), the const rbegin() is (2), and crbegin() is (3). Slay 🫢

Returns a reverse iterator to the first element of the reversed flat_map. It corresponds to the last element of the non-reversed flat_map. If the flat_map is empty, the returned iterator is equal to rend().

There's also a diagram named range-rbegin-rend.svg in the original β€” very visual, very range-rbegin-to-rend 🌈

🫢 Parameters 🫢

(none) β€” literally nothing to pass, bestie πŸŽ€

✨ Return value ✨

Reverse iterator to the first element.

πŸ”₯ Complexity πŸ”₯

Constant.

πŸ’œ 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.

⚠️ Translation: this reverse iterator is riding on the end iterator, so if the end iterator gets invalidated, your returned iterator gets invalidated too. Don't ignore that, bestie 😭

πŸ¦‹ Example πŸ¦‹

The original page says // Run this code before the snippet, so here's the snippet:

#include <iomanip>
#include <iostream>
#include <string_view>
#include <flat_map>

int main()
{
    const std::flat_map<int, std::string_view> coins
    {
        {10, "dime"},
        {100, "dollar"},
        {50, "half dollar"},
        {5, "nickel"},
        {1, "penny"},
        {25, "quarter"}
    }; // initializer entries in name alphabetical order

    std::cout << "US coins in circulation, largest to smallest denomination:\n";
    for (auto it = coins.crbegin(); it != coins.crend(); ++it)
        std::cout << std::setw(11) << it->second << " = Β’" << it->first << '\n';
}

🌈 Output 🌈

US coins in circulation, largest to smallest denomination:
     dollar = Β’100
half dollar = Β’50
    quarter = Β’25
       dime = Β’10
     nickel = Β’5
      penny = Β’1

πŸ‘‘ See also πŸ‘‘

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

Byeee 🫢 β€” source/version is http://cppreference.com, dated 2024.06.10, for the page std::flat_map::rbegin,(3). Mwah πŸ’‹

πŸ“¦ where this page comes from