Unordered_map iteration order for the same key

When navigating to std::unordered_mapSTL, it does not provide any guarantees for considering a specific order of elements.

My question is about the order of elements with the same key, I tried it with different compilers, and I always get one after the other if they have the same key (example below). I searched him, but could not find him. Is it mentioned somewhere in the standards or is implementation dependent?

unordered_multimap<int, int> umap;

umap.insert({30, 9});
umap.insert({10, 1});
umap.insert({20, 5});
umap.insert({30, 8});
umap.insert({20, 4});
umap.insert({10, 2});

for (auto p : umap)
    cout << p.first << " " << p.second << endl;

exits

30 8
30 9
20 4
20 5
10 1
10 2
+4
source share
1 answer

Yes, it is mentioned in C ++ 11 23.2.5 / 6:

In containers that support equivalent keys, items with equivalent keys are adjacent to each other in the container iteration order.

+7
source

All Articles