マップの順序を「少ない」から「多い」に変更する方法があることを誰かが知っていますか?
例えば:
map<string, int>と呼ばれるものがありtestます。私はそれにいくつかのエントリを挿入します:
test["b"] = 1;
test["a"] = 3;
test["c"] = 2;
マップ内では、順序はになります(a, 3)(b, 1)(c, 2)。
になりたいです(c, 2)(b, 1)(a, 3)。
どうすれば簡単にそれを行うことができますか?
std::greaterの代わりにキーとして使用するstd::less。
例えば
std::map< std::string, int, std::greater<std::string> > my_map;
リファレンスを参照してください
既存のマップがあり、マップの要素を逆にループしたい場合は、逆イテレータを使用します。
// This loop will print (c, 2)(b, 1)(a, 3)
for(map< string, int >::reverse_iterator i = test.rbegin(); i != test.rend(); ++i)
{
cout << '(' << i->first << ',' << i->second << ')';
}