57

I usually use C++ stdlib map whenever I need to store some data associated with a specific type of value (a key value - e.g. a string or other object). The stdlib map implementation is based on trees which provides better performance (O(log n)) than the standard array or stdlib vector.

My questions is, do you know of any C++ "standard" hashtable implementation that provides even better performance (O(1))? Something similar to what is available in the Hashtable class from the Java API.

Marcos Bento
  • 2,030
  • 4
  • 22
  • 19

9 Answers9

82

If you're using C++11, you have access to the <unordered_map> and <unordered_set> headers. These provide classes std::unordered_map and std::unordered_set.

If you're using C++03 with TR1, you have access to the classes std::tr1::unordered_map and std::tr1::unordered_set, using the same headers (unless you're using GCC, in which case the headers are <tr1/unordered_map> and <tr1/unordered_set> instead).

In all cases, there are corresponding unordered_multimap and unordered_multiset types too.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 6
    In GCC, you have to use header names and instead. It's a GCC quirk. :-) – C. K. Young Sep 25 '08 at 14:24
  • The VS2008 Feature Pack has been superseded by SP1. – Ferruccio Sep 25 '08 at 14:42
  • IIRC the VC9 Feature Pack and SP1 tr1::unordered_* implementations came wih release notes warninig of sub-optimal performance. I would assume that this will be fixed eventually. – jwfearn Sep 25 '08 at 15:22
  • Thanks, Ferruccio! I've incorporated your fix. (I apologise for misspelling your name in the edit comment, but I don't believe I now have a way to fix that.) – C. K. Young Sep 25 '08 at 22:40
16

If you don't already have unordered_map or unordered_set, they are part of boost.
Here's the documentation for both.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Yay for Boost providing _maximal portability_ once again. :-) See http://stackoverflow.com/questions/131445 for another instance of this. – C. K. Young Sep 25 '08 at 22:43
3

There is a hash_map object as many here have mentioned, but it is not part of the stl. It is a SGI extension, so if you were looking for something in the STL, I think you are out of luck.

Craig H
  • 7,949
  • 16
  • 49
  • 61
2

Visual Studio has the class stdext::hash_map in the header <hash_map>, and gcc has the class __gnu_cxx::hash_map in the same header.

ypnos
  • 50,202
  • 14
  • 95
  • 141
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • If they have the same interface, it's pretty easy to make an implementation that supports both of them using namespace aliases and some pre-processor work. – Jasper Bekkers Mar 25 '09 at 15:31
  • I know, I know, this is deprecated stuff. Not every collegue may be convinced to switch to VS 2008 or install Boost. So here is a good trick on how to get these together: ` namespace std { #ifdef __GNUC__ using namespace __gnu_cxx; #elif using namespace stdext; #endif }` – ypnos Mar 03 '10 at 15:50
2

std::tr1::unordered_map, in <unordered_map>

if you don't have tr1, get boost, and use boost::unordered_map in <boost/unordered_map.hpp>

rlbond
  • 65,341
  • 56
  • 178
  • 228
1

See std::hash_map from SGI.

This is included in the STLPort distribution as well.

Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
  • It may be included in STLPort, but it's still not "standard", as the question sought after. There is no _standard_ class called hash_map; it's called unordered_map instead, and it's in TR1. – C. K. Young Sep 25 '08 at 14:21
1

hash_map is also supported in GNU's libstdc++.

Dinkumware also supports this, which means that lots of implementations will have a hash_map (I think even Visual C++ delivers with Dinkumware).

Ben Collins
  • 20,538
  • 18
  • 127
  • 187
  • It may be widely supported, but it's not "standard", as the questioner asked for. Only the TR1 facilities can be considered standard. – C. K. Young Sep 25 '08 at 14:23
  • The point I was making is that if all the compiler vendors support it (or stdlibc++ vendors, as the case may be), then it may be a good enough substitute for "standard". BTW, TR1 isn't "standard" yet. It's accepted for the next standard, which is enough to get vendors to implement it (my point...) – Ben Collins Sep 25 '08 at 15:56
  • I think that's just for libstdc++ version >= 4.0? – rogerdpack Dec 07 '09 at 19:04
1

If you have the TR1 extensions available for yor compiler, use those. If not, boost.org has a version that's quite similar except for the std:: namespace. In that case, put in a using-declaration so you can switch to std:: later.

MSalters
  • 173,980
  • 10
  • 155
  • 350
-1

std::hash_map

Dima
  • 38,860
  • 14
  • 75
  • 115