简体   繁体   English

相同键的unordered_map迭代顺序

[英]unordered_map iteration order for the same key

When looping over an std::unordered_map , STL makes no guarantees on which specific element order is considered. 当循环遍历std::unordered_map ,STL不保证考虑哪个特定的元素顺序。

My question is about the order of the elements with the same key, I tried it with different compilers and I always receive one after the other if they have same key (example below). 我的问题是关于具有相同密钥的元素的顺序,我尝试使用不同的编译器,并且如果它们具有相同的密钥,我总是一个接一个地接收(例如下面的例子)。 I searched it but I couldn't find. 我搜索了但我找不到。 Is it mentioned somewhere in standards or it 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;

outputs 输出

30 8
30 9
20 4
20 5
10 1
10 2

Yes, it's mentioned in C++11 23.2.5/6: 是的,它在C ++ 11 23.2.5 / 6中提到:

In containers that support equivalent keys, elements with equivalent keys are adjacent to each other in the iteration order of the container. 在支持等效键的容器中,具有等效键的元素在容器的迭代顺序中彼此相邻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM