如何在C++中查找hash_map?

Sup*_*ing 0 c++ hashmap

这就是我所拥有的,我是C++的新手,所以我不确定这是不对的......

typedef pair<string, int>:: make_pair;
hash_map <string, int> dict;
dict.insert(make_pair("apple", 5));
Run Code Online (Sandbox Code Playgroud)

我想给我的hash_map"apple",我想回来5.我该怎么做?

Tho*_*ini 9

hash_map 不是标准的C++,所以你应该查看你正在使用的任何库的文档(或者至少告诉我们它的名字),但最有可能的是:

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 */ }
Run Code Online (Sandbox Code Playgroud)

或者,如果你肯定知道它"apple"dict,你也可以这样做:dict["apple"].例如,cout << dict["apple"];将打印5.

另外,为什么代码中的typedef?你不能只使用std::make_pair?而且,它不会编译你编写它的方式(使用两个主要冒号)