c++逆序map怎么输出
时间: 2023-10-31 19:16:38 浏览: 140
C++ 给出一个多位数,使其逆序输出
逆序map可以使用反向迭代器(rbegin()和rend())进行输出。具体实现方法如下:
```c++
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> myMap;
myMap[1] = "one";
myMap[2] = "two";
myMap[3] = "three";
myMap[4] = "four";
// 使用反向迭代器输出
for (auto it = myMap.rbegin(); it != myMap.rend(); it++) {
cout << it->first << ": " << it->second << endl;
}
return 0;
}
```
输出结果为:
```
4: four
3: three
2: two
1: one
```
阅读全文