相同键的unordered_map迭代顺序

Ian*_*Ian 4 c++ c++11 c++14

当循环遍历一个时std::unordered_map,STL不保证考虑哪个特定的元素顺序.

我的问题是关于具有相同键的元素的顺序,我尝试使用不同的编译器,并且如果它们具有相同的键,我总是一个接一个地接收(例如下面的例子).我搜查了但我找不到.它是在标准的某处提到还是依赖于实现?

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;
Run Code Online (Sandbox Code Playgroud)

输出

30 8
30 9
20 4
20 5
10 1
10 2
Run Code Online (Sandbox Code Playgroud)

Mik*_*our 7

是的,它在C++ 11 23.2.5/6中提到:

在支持等效键的容器中,具有等效键的元素在容器的迭代顺序中彼此相邻.