std::map::r...:map::crend(3)

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

πŸ’– NAME πŸ’–

hey bestie, you're on the std::map::r...:map::crend(3) page from the C++ Standard Libary manual, and today's main characters are std::map::rend,std::map::crend β€” aka std::map::rend,std::map::crend πŸ’…βœ¨

🌸 Synopsis 🌸

okay so there are three overloads, no cap:

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

These return a reverse iterator to the element following the last element of the reversed map. It corresponds to the element preceding the first element of the non-reversed map. This element acts as a placeholder, and attempting to access it results in undefined behavior 😭🚨

the original page also has this diagram file: range-rbegin-rend.svg πŸ¦‹

πŸŽ€ Parameters πŸŽ€

(none) β€” zero arguments, zilch, nothing to pass, bestie 🫢

⭐ Return value ⭐

Reverse iterator to the element following the last element. that's the whole vibe πŸ’–

πŸ”₯ Complexity πŸ”₯

Constant. no scaling drama, literally constant πŸ‘‘

🌈 Notes 🌈

libc++ backports crend() to C++98 mode. cute, iconic, noted πŸ’…

πŸ“ Example πŸ“

// Run this code

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

using namespace std::chrono;

int main()
{
    const std::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 (C++11)
  • rend β€” returns a reverse end iterator for a container or array
  • crend β€” function template (C++14)

source/version check, bestie: http://cppreference.com, dated 2024.06.10 β€” reverse-iterate responsibly and slay the day πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from