Skip to main content

std::multimap rend() method

// Non const version
reverse_iterator rend() noexcept;

// Const version
reverse_iterator rend() const noexcept;

// Const version
const_reverse_iterator crend() const noexcept;

It corresponds to the last element of the non-reversed vector.

Returns a reverse iterator

to the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector.

It effectively returns an iterator that points past the end of the original vector. Attempting to dereference a past-the-end iterator is undefined behaviour

.

note

This method doesn't actually reverse the vector, it just returns an iterator that points to the element before the first element of the array, and whose +, -, --, ++ operators have slightly changed implementations.

For example it++ decrements the internal pointer and it-- increments it (so that traversing the container in a reverse order actually works).

If the container is empty, the returned iterator will be equal to rbegin().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1).

Difference between rend and crend

For a const container c, rend and crend are the same - c.rend() == c.crend()

For non-const container of type c they return different iterators:

#include <map> 

int main()
{
std::multimap<int, float> multimap = { {1, 1.f}, {2, 3.f}, {5, 8.f} };
auto it = arr.rend(); // Type: std::multimap<int, float>::reverse_iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <iostream>
#include <iomanip>
#include <chrono>
#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::multimap<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';
}
}
Output
Messages received, reverse date order:
2023/02/17 : 20
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 : 24
2020/04/01 : 42
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::multimap rend() method

// Non const version
reverse_iterator rend() noexcept;

// Const version
reverse_iterator rend() const noexcept;

// Const version
const_reverse_iterator crend() const noexcept;

It corresponds to the last element of the non-reversed vector.

Returns a reverse iterator

to the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector.

It effectively returns an iterator that points past the end of the original vector. Attempting to dereference a past-the-end iterator is undefined behaviour

.

note

This method doesn't actually reverse the vector, it just returns an iterator that points to the element before the first element of the array, and whose +, -, --, ++ operators have slightly changed implementations.

For example it++ decrements the internal pointer and it-- increments it (so that traversing the container in a reverse order actually works).

If the container is empty, the returned iterator will be equal to rbegin().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1).

Difference between rend and crend

For a const container c, rend and crend are the same - c.rend() == c.crend()

For non-const container of type c they return different iterators:

#include <map> 

int main()
{
std::multimap<int, float> multimap = { {1, 1.f}, {2, 3.f}, {5, 8.f} };
auto it = arr.rend(); // Type: std::multimap<int, float>::reverse_iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <iostream>
#include <iomanip>
#include <chrono>
#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::multimap<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';
}
}
Output
Messages received, reverse date order:
2023/02/17 : 20
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 : 24
2020/04/01 : 42
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.