开发者

std::hash_set vs std::unordered_set, are they the same thing?

开发者 https://www.devze.com 2023-04-10 15:30 出处:网络
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?The comple

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?


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.


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 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号