53

I don't know how to use the hash function in C++, but I know that we can use hash_map. Does g++ support that by simply including #include <hash_map>? What is a simple example using hash_map?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
skydoor
  • 25,218
  • 52
  • 147
  • 201
  • 8
    @BlueRaja: Yes indeed, using it leads to such wonderful confusion as people believing that `hash_map` is actually part of C++. I think this, like any other programming-related question, is right at home here on SO, don't you? Telling people to use Google is a waste of theirs and our time. Theirs because they have to sift through thousands of inaccurate and unreliable answers, and ours because we then have to tear down all their misconceptions and bad practices when they come here to ask questions later. – jalf Feb 02 '10 at 00:08
  • 1
    @jalf: as if answers on SO were somehow guaranteed to be accurate and reliable. – just somebody Feb 02 '10 at 15:14
  • 11
    They are guaranteed to be seen and voted on by other programmers, which makes them a hell of a lot more trustworthy than most of what a beginner might find blindly searching on Google. – jalf Feb 02 '10 at 15:25

5 Answers5

55

The current C++ standard does not have hash maps, but the coming C++0x standard does, and these are already supported by g++ in the shape of "unordered maps":

#include <unordered_map>
#include <iostream>
#include <string>
using namespace std;

int main() {
    unordered_map <string, int> m;
    m["foo"] = 42;
    cout << m["foo"] << endl;
}

In order to get this compile, you need to tell g++ that you are using C++0x:

g++ -std=c++0x main.cpp

These maps work pretty much as std::map does, except that instead of providing a custom operator<() for your own types, you need to provide a custom hash function - suitable functions are provided for types like integers and strings.

  • 2
    @Kornel No, I shouldn't. TR1 was never ratified - the tr1 namespace is just an extension some compiler provide. I never use it in my own code. –  Feb 03 '10 at 15:32
  • @Neil, sooo, a person using GCC 3.4, MSVC and other non-C++0x compilers should not use TR1, but gratiously wait until C++0x library extensions are supported out of the box? – Kornel Kisielewicz Feb 03 '10 at 15:35
  • @Kornel They certainly can use TR1, but I don't use VC++ so cannot in all conscience edit my answer to suggest they do so. To be really safe, which is what most of my code does, don't use unordered maps. It depends how portable you want your code to be. –  Feb 03 '10 at 15:40
  • @Neil, hmm, point taken, however in that case, wouldn't it be acceptable to point to Boost.Unordered, that is added to the working draft of the C++0x standard? – Kornel Kisielewicz Feb 03 '10 at 15:48
  • Std now has a `map` data structure. – Chris Feb 20 '17 at 20:48
11

#include <tr1/unordered_map> will get you next-standard C++ unique hash container. Usage:

std::tr1::unordered_map<std::string,int> my_map;
my_map["answer"] = 42;
printf( "The answer to life and everything is: %d\n", my_map["answer"] );
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
5

Wikipedia never lets down:

http://en.wikipedia.org/wiki/Hash_map_(C%2B%2B)

Manuel
  • 12,749
  • 1
  • 27
  • 35
4

hash_map is a non-standard extension. unordered_map is part of std::tr1, and will be moved into the std namespace for C++0x. http://en.wikipedia.org/wiki/Unordered_map_%28C%2B%2B%29

Ben
  • 1,298
  • 11
  • 12
1

The name accepted into TR1 (and the draft for the next standard) is std::unordered_map, so if you have that available, it's probably the one you want to use.

Other than that, using it is a lot like using std::map, with the proviso that when/if you traverse the items in an std::map, they come out in the order specified by operator<, but for an unordered_map, the order is generally meaningless.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111