Namespaces
Variants
Views
Actions

std::map<Key,T,Compare,Allocator>::rend, std::map<Key,T,Compare,Allocator>::crend

From cppreference.com
< cpp‎ | container‎ | map
reverse_iterator rend();
(until C++11)
reverse_iterator rend() noexcept;
(since C++11)
const_reverse_iterator rend() const;
(until C++11)
const_reverse_iterator rend() const noexcept;
(since C++11)
const_reverse_iterator crend() const noexcept;
(since C++11)

Returns 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, attempting to access it results in undefined behavior.

range-rbegin-rend.svg

Contents

[edit] Parameters

(none)

[edit] Return value

Reverse iterator to the element following the last element.

[edit] Complexity

Constant.


[edit] Example

#include <iostream>
#include <iomanip>
#include <chrono>
#include <map>
#include <string_view>
using namespace std::chrono;
 
// untill C++20 chrono operator<< ready
std::ostream& operator<<(std::ostream& os, const year_month_day& ymd) {
    return os << std::setfill('0') << static_cast<int>(ymd.year()) << '/'
              << std::setw(2) << static_cast<unsigned>(ymd.month()) << '/'
              << std::setw(2) << static_cast<unsigned>(ymd.day());
}
 
int main() 
{
    const std::map<year_month_day, int> messages {
        {February/17/2023, 122},
        {February/16/2022, 100}, 
        {October/22/2022, 90}, 
        {June/14/2022, 88},
        {November/23/2021, 78}, 
        {December/10/2022, 77},
        {December/12/2021, 42},
        {April/1/2020, 17}, 
        };
    std::cout << "Messages received, reverse date order:\n";
    for (auto it = messages.crbegin(); it != messages.crend(); ++it) {
        std::cout << it->first << " : " << it->second << '\n';
    }
}

Possible output:

Messages received, reverse date order:
2023/02/17 : 122
2022/12/10 : 77
2022/10/22 : 90
2022/06/14 : 88
2022/02/16 : 100
2021/12/12 : 42
2021/11/23 : 78
2020/04/01 : 17

[edit] See also

returns a reverse iterator to the beginning
(public member function) [edit]