Spec-Zone.ru › C++

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

(1)
reverse_iterator rend();
(до C++11)
reverse_iterator rend() noexcept;
(с C++11)
(2)
const_reverse_iterator rend() const;
(до C++11)
const_reverse_iterator rend() const noexcept;
(с C++11)
const_reverse_iterator crend() const noexcept;
(3) (с C++11)

Возвращает обратный итератор к элементу, следующему за последним элементом обращённой map. Он соответствует элементу, предшествующему первому элементу необращённой map. Этот элемент используется как маркер; попытка получить к нему доступ приведёт к неопределённому поведению.

range-rbegin-rend.svg

Параметры

(нет)

Возвращаемое значение

Обратный итератор к элементу, следующему за последним элементом.

Сложность

Постоянная.

Пример

#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <string_view>
using namespace std::chrono;
 
// until 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, 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, reverse date order:\n";
    for (auto it = messages.crbegin(); it != messages.crend(); ++it)
        std::cout << it->first << " : " << it->second << '\n';
}

Возможный вывод:

Messages received, reverse date order:
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

См. также

rbegincrbegin
(C++11)
возвращает обратный итератор к началу
(публичный член-функция)
rendcrend
(C++14)
возвращает обратный итератор в конец контейнера или массива
(шаблон функции)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/container/map/rend

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API