Can someone give an example how to use hash_map in C++ with string as a key and integer as a value? Is something like hash_map h; a valid syntax? Do I explicitly have to define a hashing function or there is a predifined one for string keys?
1 Answer
Take a look at the documentation here, the example shown uses strings as keys and int as values (sometimes it is better to just check the docs first then ask here on so : D).
To be more precise:
- you have to specify the key type, the value type, a hash function and a comparison function in the template
- you can either construct your own hash function or use a default functor provided by the libs by instantiating a template for hash
- for accessing the values just use the keys as if they were the index of an array
(I'm not pasting the code, you can find it in the documentation)
2 Comments
Angew is no longer proud of SO
Do you think the test environment will happen to have technology as outdated as the STL installed?
rano
I do not know, I was sticking to the question desiderata : ) (Btw the story is the same, just check the documentation first)
std::unordered_map, TR1 hasstd::tr1::unordered_map, Boost hasboost::unordered_map. If the test environment supports neither, you'd have to implement your own.