Keep the order of unordered_map as we insert a new key

后端 未结 3 2102
抹茶落季
抹茶落季 2021-02-19 07:01

I inserted the elements to the unordered_map with this code:

    myMap.insert(std::make_pair(\"A\", 10));
    myMap.insert(std::make_pair(\"B\", 11         


        
3条回答
  •  天命终不由人
    2021-02-19 08:04

    No, it is not possible.

    Usage of std::unordered_map doesn't give you any guarantee on element order.

    If you want to keep elements sorted by map keys (as seems from your example) you should use std::map.

    If you need to keep list of ordered pairs you can use std::vector>.

提交回复
热议问题