std::hash_set 与 std::unordered_set,它们是同一件事吗?
我知道 hash_set
是非标准的,而 unordered_set
是标准的。但是,我想知道,就性能而言,两者之间有什么区别?为什么它们单独存在?
I know hash_set
is non-standard and unordered_set
is standard. However, I am wondering, performance wise, what is the difference between the two? Why do they exist separately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 标准规定的 unordered_ 容器的复杂性要求基本上没有为实现留下太多空间,它必须是某种哈希表。该标准是在充分意识到这些数据结构已被大多数供应商部署为扩展的情况下编写的。
编译器供应商通常将这些容器称为“哈希映射”或“哈希集”,这可能就是您所指的(标准中没有文字
std::hash_set
,但我认为有GCC 中的一个位于单独的命名空间中,对于其他编译器也类似)。在编写新标准时,作者希望避免与现有扩展库可能发生混淆,因此他们选择了一个反映典型 C++ 思维方式的名称:说明它是什么,而不是说明它是如何实现的。无序容器是无序的。这意味着与订购的容器相比,您从它们中获得的资源较少,但这种实用性的降低使您可以更有效地访问。
在实现方面,hash_set、Boost-unordered、TR1-unordered 和 C++11-unordered 即使不相同,也非常相似。
The complexity requirements for the
unordered_
-containers set out by the C++ standard essentially don't leave much room for the implementation, which has to be some sort of hash table. The standard was written in full awareness that those data structures had already been deployed by most vendors as an extension.Compiler vendors would typically call those containers "hash map" or "hash set", which is what you're probably referring to (there is no literal
std::hash_set
in the standard, but I think there's one in GCC in a separate namespace, and similarly for other compilers).When the new standard was written, the authors wanted to avoid possible confusion with existing extension libraries, so they went for a name that reflects the typical C++ mindset: say what it is, not how it's implemented. The unordered containers are, well, unordered. That means you get less from them compared to the ordered containers, but this diminished utility affords you more efficient access.
Implementation-wise, hash_set, Boost-unordered, TR1-unordered and C++11-unordered will be very similar, if not identical.
关于主题行中的“它们是同一件事吗”的问题:根据我将代码从 __gnu_cxx::hash_set 升级到 std::unordered_set 的经验,它们几乎但不完全是同一件事。
我遇到的区别是,迭代 __gnu_cxx::hash_set 返回的项目似乎是原始插入顺序,而 std::unordered_set 则不会。因此,顾名思义,在迭代整个 std::unordered_set 时,不能依赖迭代器以任何特定顺序返回项目。
Regarding the question "are they the same thing" from the subject line: based on my experience of upgrading code from __gnu_cxx::hash_set to std::unordered_set, they are almost, but not exactly, the same thing.
The difference that I ran into is that iterating through __gnu_cxx::hash_set returned the items in what appeared to be the original order of insertion, whereas std::unordered_set would not. So as the name implies, one cannot rely on an iterator to return the items in any particular order when iterating though the entire std::unordered_set.
例如,Visual Studio 2010 同时具有
hash_xxx
和unordered_xxx
,如果您查看标头,至少它们的实现对于所有这些都是相同的(相同的 base-/"policy “-类)。对于其他编译器,我不知道,但由于哈希容器通常必须如何实现,我想不会有太多差异(如果有的话)。
Visual Studio 2010 for example has both
hash_xxx
andunordered_xxx
, and if you look through the headers, atleast their implementation is the same for all of those (same base-/"policy"-classes).For other compilers, I don't know, but due to how hash container usually have to be implemented, I guess there won't be many differences, if any at all.
它们几乎是相同的东西。标准 (C++0x) 名称是 unordered_set。 hash_set 是 boost 和其他人的早期名称。
They are pretty much the same things. The standard (C++0x) name is unordered_set. hash_set was an earlier name from boost and others.