跳到主要内容

std::multimap rend() 方法

// Non const version
reverse_iterator rend() noexcept;

// Const version
reverse_iterator rend() const noexcept;

// Const version
const_reverse_iterator crend() const noexcept;

它对应于未反转向量的最后一个元素。

返回一个反向迭代器

它对应于反转向量的最后一个元素。它对应于未反转向量的第一个元素<强>之前的元素。

它实际上返回一个指向原始向量末尾之后的迭代器。尝试解引用末尾之后的迭代器是未定义行为

.

注意

此方法实际上并未反转向量,它只是返回一个迭代器,该迭代器指向数组第一个元素之前的元素,并且其 +---++ 运算符的实现略有不同。

例如,it++会递减内部指针,而it--会递增内部指针(以便以相反的顺序遍历容器实际工作)。

如果容器为空,则返回的迭代器将等于 rbegin()

参数

(无)

返回值

指向第一个元素的反向迭代器。

复杂度

常数 - O(1)

rend 和 crend 的区别

对于常量容器c,rend 和 crend 是相同的 - c.rend() == c.crend()

对于非常量类型c的容器,它们返回不同的迭代器

#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
}

示例

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';
}
}
输出
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
本文源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::multimap rend() 方法

// Non const version
reverse_iterator rend() noexcept;

// Const version
reverse_iterator rend() const noexcept;

// Const version
const_reverse_iterator crend() const noexcept;

它对应于未反转向量的最后一个元素。

返回一个反向迭代器

它对应于反转向量的最后一个元素。它对应于未反转向量的第一个元素<强>之前的元素。

它实际上返回一个指向原始向量末尾之后的迭代器。尝试解引用末尾之后的迭代器是未定义行为

.

注意

此方法实际上并未反转向量,它只是返回一个迭代器,该迭代器指向数组第一个元素之前的元素,并且其 +---++ 运算符的实现略有不同。

例如,it++会递减内部指针,而it--会递增内部指针(以便以相反的顺序遍历容器实际工作)。

如果容器为空,则返回的迭代器将等于 rbegin()

参数

(无)

返回值

指向第一个元素的反向迭代器。

复杂度

常数 - O(1)

rend 和 crend 的区别

对于常量容器c,rend 和 crend 是相同的 - c.rend() == c.crend()

对于非常量类型c的容器,它们返回不同的迭代器

#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
}

示例

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';
}
}
输出
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
本文源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。