如何根据 c++ 中键的递增/减顺序打印哈希映射的值

How to print value of a hash map according to increasing/decreasing order of key in c++?

本文关键字:顺序 打印 哈希 映射 何根 c++      更新时间:2023-10-16

如果我们有一张地图,

map <int,int> m;

和以下键值对 (-2,3) , (-14,8) , (4,8), (6,12) , (3,76)

现在,如果我们想按键的递增顺序打印值,那么如何打印?

o/p

8

3 76 8 12

默认情况下,std::map中的键是有序的(使用 operator< )。您可以遍历地图:

for (std::map<int, int>::iterator i = m.begin(); i != m.end(); i++)
{
    cout << i->second << "n";
}