std::unordered_multimap 中元素的顺序 [英] order of elements in std::unordered_multimap

查看:47
本文介绍了std::unordered_multimap 中元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下代码

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));

如果我使用迭代器访问值,它们将始终按以下顺序排列:v1, v2, 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?

推荐答案

我想这是特定于实现的.在 unordered_multimap 中,如果实现是存储桶哈希映射,则具有相同键的元素存储在同一个存储桶中,在这种情况下,它们的插入顺序可能相同(这可能是您的情况).

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).

但是在一个 unordered_map 实现中,例如,使用开放寻址技术,顺序可能会改变.我不知道是否有使用不同底层实现的 STL 实现,但是类的契约没有对同一个键的值的顺序做出任何假设,所以我认为你不能把它当作授予.

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.

取自此处:

在内部,unordered_map 中的元素没有按任何特定顺序对其键或映射值进行排序

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values

这篇关于std::unordered_multimap 中元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆