I would like to see a hash_map example in C++

105,006

Solution 1

#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"] );

Solution 2

Wikipedia never lets down:

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

Solution 3

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

Solution 4

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.

Share:
105,006
skydoor
Author by

skydoor

Updated on July 13, 2022

Comments

  • skydoor
    skydoor almost 2 years

    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?

  • Bill
    Bill over 14 years
    C-style output with C++-style containers? Ouch!
  • Nikolai Fetissov
    Nikolai Fetissov over 14 years
    Welcome to the real world :)