sorting in std::map where key is a std::string

后端 未结 4 1426
-上瘾入骨i
-上瘾入骨i 2021-02-01 18:20

I have a std::map mymap

Now, if I insert values in the map like:

std::map  mymap;
mymap[\"first\"] = \"hi\";
mymap[\"third\"] = \"         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 18:54

    The map is actually a tree, and is sorted by KEY order. You are printing itr->second which is the VALUE not the KEY. If you want your key/value pairs sorted by VALUE, use the VALUE as key instead, or store everything in another container (say an array), then sort them.

提交回复
热议问题