阅读量:132
在C++中,可以通过使用rbegin()和rend()函数来对unordered_map进行反向遍历。以下是一个示例代码:
#include
#include
int main() {
std::unordered_map<int, std::string> myMap = {
{1, "apple"},
{2, "banana"},
{3, "orange"}
};
// 反向遍历unordered_map
for (auto it = myMap.rbegin(); it != myMap.rend(); ++it) {
std::cout << "Key: " << it>first << ", Value: " << it>second << std class="hljs-keyword">return 0;
}
在上面的示例中,我们首先创建了一个unordered_map对象myMap,然后使用rbegin()和rend()函数来对其进行反向遍历,打印出每个键值对的键和值。