在C ++ STL中set和hashset之间有什么区别? [英] What is the difference between set and hashset in C++ STL?
问题描述
我应该什么时候选择一个?
是否有任何建议使用正确的STL容器的指针?
When should I choose one over the other? Are there any pointers that you would recommend for using the right STL containers?
推荐答案
hash_set
是不是C ++标准的一部分的扩展。对于 set
,查找应该是O(1)而不是O(log n),因此在大多数情况下它会更快。
hash_set
is an extension that is not part of the C++ standard. Lookups should be O(1) rather than O(log n) for set
, so it will be faster in most circumstances.
当你遍历容器时,会看到另一个区别。 set
将以排序顺序传递内容,而 hash_set
将基本上是随机的(感谢Lou Franco)。
Another difference will be seen when you iterate through the containers. set
will deliver the contents in sorted order, while hash_set
will be essentially random (Thanks Lou Franco).
编辑:C ++ 11对C ++标准的更新引入了 unordered_set
,而不是 hash_set
。性能将类似,并由标准保证。名称中的无序强调迭代它将产生没有特定顺序的结果。
The C++11 update to the C++ standard introduced unordered_set
which should be preferred instead of hash_set
. The performance will be similar and is guaranteed by the standard. The "unordered" in the name stresses that iterating it will produce results in no particular order.
这篇关于在C ++ STL中set和hashset之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!