跳到主要内容

std::multimap rbegin() 方法

// Non const version
reverse_iterator rbegin() noexcept;

// Const version
reverse_iterator rbegin() const noexcept;

// Const version
const_reverse_iterator crbegin() const noexcept;

返回一个反向迭代器

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

注意

此方法实际上不会反转 vector,它只是返回一个指向 vector 最后一个元素的迭代器,并且其 +---++ 运算符的实现略有不同。

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

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

参数

(无)

返回值

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

复杂度

常数 - O(1)

rbegin 和 crbegin 之间的区别

对于 const 容器 c,rbegin 和 crbegin 相同 - c.rbegin() == c.crbegin()

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

#include <map>

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

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <map>
#include <string>

int main()
{
std::multimap<std::string, int> multimap {
{ "█", 1 },
{ "▒", 5 },
{ "░", 3 },
{ "▓", 7 },
{ "▓", 8 },
{ "░", 4 },
{ "▒", 6 },
{ "█", 2 },
};

std::cout << "Print out in reverse order using const reverse iterators:\n";
std::for_each(multimap.crbegin(), multimap.crend(),
[](std::pair<const std::string, int> const& e) {
std::cout << "{ \"" << e.first << "\", " << e.second << " };\n";
});

multimap.rbegin()->second = 42; // OK: non-const value is modifiable
// multimap.crbegin()->second = 42; // Error: can't modify the const value
}
输出
Print out in reverse order using const reverse iterators:
{ "▓", 8 };
{ "▓", 7 };
{ "▒", 6 };
{ "▒", 5 };
{ "░", 4 };
{ "░", 3 };
{ "█", 2 };
{ "█", 1 };
本文源自 此 CppReference 页面。它可能已为改进或编辑偏好而进行更改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::multimap rbegin() 方法

// Non const version
reverse_iterator rbegin() noexcept;

// Const version
reverse_iterator rbegin() const noexcept;

// Const version
const_reverse_iterator crbegin() const noexcept;

返回一个反向迭代器

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

注意

此方法实际上不会反转 vector,它只是返回一个指向 vector 最后一个元素的迭代器,并且其 +---++ 运算符的实现略有不同。

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

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

参数

(无)

返回值

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

复杂度

常数 - O(1)

rbegin 和 crbegin 之间的区别

对于 const 容器 c,rbegin 和 crbegin 相同 - c.rbegin() == c.crbegin()

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

#include <map>

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

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <map>
#include <string>

int main()
{
std::multimap<std::string, int> multimap {
{ "█", 1 },
{ "▒", 5 },
{ "░", 3 },
{ "▓", 7 },
{ "▓", 8 },
{ "░", 4 },
{ "▒", 6 },
{ "█", 2 },
};

std::cout << "Print out in reverse order using const reverse iterators:\n";
std::for_each(multimap.crbegin(), multimap.crend(),
[](std::pair<const std::string, int> const& e) {
std::cout << "{ \"" << e.first << "\", " << e.second << " };\n";
});

multimap.rbegin()->second = 42; // OK: non-const value is modifiable
// multimap.crbegin()->second = 42; // Error: can't modify the const value
}
输出
Print out in reverse order using const reverse iterators:
{ "▓", 8 };
{ "▓", 7 };
{ "▒", 6 };
{ "▒", 5 };
{ "░", 4 };
{ "░", 3 };
{ "█", 2 };
{ "█", 1 };
本文源自 此 CppReference 页面。它可能已为改进或编辑偏好而进行更改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。