unixman83
unixman83

Reputation: 9933

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

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?

Upvotes: 19

Views: 24072

Answers (4)

John Hinrichsen
John Hinrichsen

Reputation: 259

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.

Upvotes: 4

Xeo
Xeo

Reputation: 131789

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.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477040

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.

Upvotes: 26

David Nehme
David Nehme

Reputation: 21572

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.

Upvotes: 1

Related Questions