std::hash_set 与 std::unordered_set,它们是同一件事吗?

发布于 2024-12-07 14:13:00 字数 107 浏览 0 评论 0原文

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不乱于心 2024-12-14 14:13:00

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.

被翻牌 2024-12-14 14:13:00

关于主题行中的“它们是同一件事吗”的问题:根据我将代码从 __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.

若无相欠,怎会相见 2024-12-14 14:13:00

例如,Visual Studio 2010 同时具有 hash_xxxunordered_xxx,如果您查看标头,至少它们的实现对于所有这些都是相同的(相同的 base-/"policy “-类)。
对于其他编译器,我不知道,但由于哈希容器通常必须如何实现,我想不会有太多差异(如果有的话)。

Visual Studio 2010 for example has both hash_xxx and unordered_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.

好倦 2024-12-14 14:13:00

它们几乎是相同的东西。标准 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文