hash_map
is not standard C++ so you should check out the documentation of whatever library you're using (or at least tell us its name), but most likely this will work:
hash_map<string, int>::iterator i = dict.find("apple");
if (i == dict.end()) { /* Not found */ }
else { /* i->first will contain "apple", i->second will contain 5 */ }
Alternatively, if you know for sure that "apple"
is in dict
, you can also do: dict["apple"]
. For example cout << dict["apple"];
will print out 5.
Also, why the typedef in your code? Can't you just use std::make_pair
? And, it won't compile the way you wrote it (with the two leading colons)