std::flat_map::rend,(3)

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

πŸŽ€ NAME πŸŽ€

okay so bestie, welcome to std::flat_map::rend,(3) from the C++ Standard Libary manual ✨ β€” yes, spelling as-is, we don't edit the header, we respect the header πŸ’…. The NAME line is literally std::flat_map::rend, - std::flat_map::rend, β€” she said her name twice, main character energy πŸ‘‘.


πŸ“œ Synopsis πŸ“œ

std::flat_map::rend has three overloads, all (since C++23):

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 she do? She returns a reverse iterator to the element following the last element of the reversed flat_map πŸŒ€. That iterator corresponds to the element preceding the first element of the non-reversed flat_map πŸ’«. This element is a placeholder, and attempting to access it results in undefined behavior β€” so do not touch, bestie, fr fr 😭🚫.

There's also a diagram named range-rbegin-rend.svg β€” iconic visual aid, no notes ✨.


🧸 Parameters 🧸

  • (none) β€” no parameters, she's low-maintenance 🫢

πŸ’– Return value πŸ’–

Reverse iterator to the element following the last element. Periodt.


⚑ Complexity ⚑

Constant. That's it, no cap πŸ”₯.


🌸 Example 🌸

Here's the code, exactly as the page gives it:

// Run this code

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

using namespace std::chrono;

int main()
{
    const std::flat_map<year_month_day, int> messages
    {
        {February/17/2023, 10},
        {February/17/2023, 20},
        {February/16/2022, 30},
        {October/22/2022, 40},
        {June/14/2022, 50},
        {November/23/2021, 60},
        {December/10/2022, 55},
        {December/12/2021, 45},
        {April/1/2020, 42},
        {April/1/2020, 24}
    };

    std::cout << "Messages received (date order is reversed):\n";
    for (auto it = messages.crbegin(); it != messages.crend(); ++it)
        std::cout << it->first << " : " << it->second << '\n';
}

πŸ“ Possible output πŸ“

Messages received (date order is reversed):
2023-02-17 : 10
2022-12-10 : 55
2022-10-22 : 40
2022-06-14 : 50
2022-02-16 : 30
2021-12-12 : 45
2021-11-23 : 60
2020-04-01 : 42

πŸ‘€ See also πŸ‘€

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

Cute sign-off time 🫢: this page's footer says source http://cppreference.com, date/version 2024.06.10, and page tag std::flat_map::rend,(3) 🌈. Bye bestie, go forth and reverse-iterate responsibly πŸ’œ.

πŸ“¦ where this page comes from