If I have the following piece of code
std::unordered_multimap<std::string, std::vector<double>> myMap;
std::vector<double> v1, v2, v3;
// init v1, v2, v3....
myMap.insert(std::make_pair<std::string, std::vector<double>("vec", v1));
myMap.insert(std::make_pair<std::string, std::vector<double>("vec", v2));
myMap.insert(std::make_pair<std::string, std::vector<double>("vec", v3));
If I access the values with an iterator they will always be in this order: v1, v2, v3
So basically if I insert elements of the same key, but different value, do they always retain the order of insertion?
I guess this is implementation specific. In an unordered_multimap
elements with same key are stored in the same bucket if the implementation is a bucket hash map, in this case they could be in the same order of insertion (that is probably your situation).
But in an unordered_map
implemented, for example, with an open addressing technique, the order could change. I don't know if there are STL implementations which uses different under the hood implementation but the contract of the class doesn't make any assumption on the order of the values for the same key so I don't think you can take it for granted.
Taken from here:
Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With