5

I have a variety of textbooks that note that there wasn't a hash container originally incorporated into the STL, but most also say that most standard library implementations do have some form of hash container since it was a known shortfall.

These books aren't necessarily as up to date as possible, and I got a little confused about what was really "standard" when googling, so:

At this point in time, do most standard library implementations provide a fairly consistent hash container, and if so, is it considered a part of the STL?

What's the "standard" header for this container? (I'm guessing it's #include <hash>, but just in case!).

Is there a hash set and hash map separately defined for use?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
John Humphreys
  • 37,047
  • 37
  • 155
  • 255

5 Answers5

10

The new standard hash map container is called unordered_map. You can include it in your files by #include <unordered_map>. It is part of the now-approved standard C++11.

Before C++11, you had various things like hash_map which were (widely?) supported by some vendors so you could use them, but if you did, your code wasn't really portable because hash_map wasn't part of the standard. hash_map and all the other vendor-specific versions of a hash table structure shouldn't be used now.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 1
    don't forget the quasi standards `boost::unordered_map` and co, which were the experimentation ground for the current standard and thus provide an easy transition. – Matthieu M. Aug 24 '11 at 14:22
6

See C++11 types:

  • unordered_map
  • unordered_set
  • unordered_multimap
  • unordered_multiset
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
2

Not in the current standard (03), you can use boost::unordered_map. In the new standard there will be, std::unordered_map.

Nim
  • 33,299
  • 2
  • 62
  • 101
1

The new C++11 standard has hash-equivalent containers. They are not in most implementations at this time, but they have been in TR1 extensions to the standard library, which are provided with some implementations.

AndrzejJ
  • 720
  • 5
  • 11
1

There are some hash containers in the TR1 namespace. See for example a short intro here: http://drdobbs.com/184402066

Anton Daneyko
  • 6,528
  • 5
  • 31
  • 59