简体   繁体   English

用于快速查找的最佳STL容器

[英]Best STL container for fast lookups

I need to store a list of integers, and very quickly determine if an integer is already in the list. 我需要存储一个整数列表,并很快确定整数是否已经在列表中。 No duplicates will be stored. 不会存储重复项。

I won't be inserting or deleting any values, but I will be appending values (which might be implemented as inserting). 我不会插入或删除任何值,但我将附加值(可能实现为插入)。

What is the best STL container class for this? 什么是最好的STL容器类? I found std::multimap on Google, but I that seems to require a key/value pair, which I don't have. 我在Google上找到了std::multimap ,但我似乎需要一个键/值对,我没有。

I'm fairly new to STL. 我对STL很新。 Can I get some recommendations? 我能得到一些建议吗?

Instead of a map, you can use a set when the value and the key aren't separate. 当值和键不是分开的时,您可以使用集合而不是映射。

Instead of a multiset/-map, you can use the non-multi version which doesn't store duplicates. 您可以使用不存储重复项的非多版本,而不是multiset / -map。

Instead of a set, you have the std::unordered_set as an alternative. 而不是集合,您可以使用std::unordered_set作为替代。 It may be faster for your use case. 你的用例可能会更快。

There are other, less generic, data structures that can be used to represent sets of integers, and some of those may be more efficient depending on the use case. 还有其他一些不太通用的数据结构可用于表示整数集,其中一些可能更有效,具体取决于用例。 But those other data structures aren't necessarily provided for you by the standard library. 但标准库不一定为您提供其他数据结构。

But I'm not clear which have the fastest lookup. 但我不清楚哪个查找速度最快。

Unordered set has better asymptotic complexity for lookups than the ordered one. 对于查找而言,无序集具有比有序集更好的渐近复杂度。 Whether it is faster for your use case, you can find out by measuring. 无论您的用例更快,您都可以通过测量找到答案。

not likely to exceed a thousand or so 不可能超过一千左右

In that case, asymptotic complexity is not necessarily relevant. 在这种情况下,渐近复杂性并不一定相关。

Especially for small-ish sets like this, a sorted vector can be quite efficient. 特别是对于像这样的小型集合,有序矢量可以非常有效。 Given that you "won't be inserting or deleting any values" , the vector shouldn't have significant downsides either. 鉴于您“不会插入或删除任何值” ,矢量也不应该有明显的缺点。 The standard library doesn't provide a set container implemented internally using a sorted vector, but it does provide a vector container as well as all necessary algorithms. 标准库不提供使用有序向量在内部实现的集合容器,但它确实提供了向量容器以及所有必需的算法。

I don't know how the containers compute hashes. 我不知道容器如何计算哈希值。

You can provide a hash function for the unordered container. 您可以为无序容器提供哈希函数。 By default it uses std::hash . 默认情况下,它使用std::hash std::hash uses an implementation defined hash function. std::hash使用实现定义的哈希函数。

std::unordered_set<int> is a good choice for keeping track of duplicates of int s, since both insertion and lookup can be achieved in constant time on average. std::unordered_set<int>是跟踪int的重复的一个很好的选择,因为插入和查找都可以在平均的恒定时间内实现。


Insertion 插入

Inserting a new int into the collection can be achieved with the insert() member function: 使用insert()成员函数可以实现在集合中插入新的int

std::unordered_set<int> s;
s.insert(7);

Lookup 抬头

Checking whether a given int is present in the collection can be done with the find() member function: 检查集合中是否存在给定的int可以使用find()成员函数完成:

bool is_present(const std::unordered_set<int>& s, int value) {
   return s.find(value) != s.end();
}

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

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