如何更改std :: map的顺序?

Ale*_*Lee 4 c++ map

有没有人知道有没有办法让我可以将地图顺序从"更少"改为"更多"?

例如:

有一个map<string, int>test.我插入一些条目:

test["b"] = 1;
test["a"] = 3;
test["c"] = 2;
Run Code Online (Sandbox Code Playgroud)

在地图内,订单将是(a, 3)(b, 1)(c, 2).

我想要它(c, 2)(b, 1)(a, 3).

我怎么能这么简单地做到这一点?

pmr*_*pmr 9

通过使用std::greater您的密钥而不是std::less.

例如

std::map< std::string, int, std::greater<std::string> > my_map;
Run Code Online (Sandbox Code Playgroud)

参见参考文献