简体   繁体   English

在集合中查找最快的 C++ 容器是什么<integer, std::string></integer,>

[英]What is the fastest C++ container for lookup in a collection of <integer, std::string>

Lookup is to be done either by integer or by string.查找将通过 integer 或字符串完成。 Both integer and strings are guaranteed to be unique in the collection. integer 和字符串都保证在集合中是唯一的。 The size of the collection is ~500 of those pairs.该系列的大小约为 500 对。

The interface of the collection would be 2 functions, one to translate the integer to string and one to translate the string to the corresponding integer.集合的接口将是 2 个函数,一个将 integer 转换为字符串,一个将字符串转换为相应的 integer。

It seems there is no methodology to pick up the "proper" container.似乎没有方法来挑选“合适的”容器。

An idea would be to use std::vector, which is the default and since it will be stored in a contiguous memory we ensure cache hits?一个想法是使用 std::vector,这是默认设置,因为它将存储在连续的 memory 中,我们确保缓存命中? But I know the size of the collection at compile time, so std::array?但是我在编译时知道集合的大小,所以 std::array?

Or since order is not important but the look up needs to be fast we can use a hash based solution like an std::map?或者因为顺序不重要但查找需要快速我们可以使用基于 hash 的解决方案,如 std::map? But then what is the key and what is the value?但是什么是关键,什么是价值呢? Or one can have 2 std::maps (std::map<int, std::string> and std::map(std::string, int)) with duplicated the information, since anyway the collection is small.或者一个人可以有 2 个 std::maps (std::map<int, std::string> and std::map(std::string, int)) 和重复的信息,因为无论如何集合很小。

I know that the ultimate answer would be to benchmark it, but I am wondering if there is any actual methodology to know what to pick based on principle.我知道最终的答案是对其进行基准测试,但我想知道是否有任何实际的方法可以根据原则知道选择什么。

std::map is a sorted associative container where keys are sorted. std::map 是一个已排序的关联容器,其中的键已排序。 Its implemented using height balanced tree where search, removal, and insertion operations have logarithmic complexity.它使用高度平衡树实现,其中搜索、删除和插入操作具有对数复杂度。

As key ordering is not required, using std::unordered_map will be more efficient.由于不需要键排序,使用 std::unordered_map 会更有效率。 Unordered map is an associative container where search, insertion, and removal of elements have average constant-time complexity. Unordered map 是一个关联容器,其中元素的搜索、插入和删除具有平均恒定时间复杂度。

Having 2 unordered_maps would give O(1) time for each search operation.拥有 2 个 unordered_maps 将为每个搜索操作提供 O(1) 时间。

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

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