#include <iostream>
#include <unordered_map>
//#include <map>
using namespace std;
//哈希表 unordered_map, 配合pair使用
void unordered_map_test()
{
unordered_map<string, int> score_umap={{ "语文", 86 },{ "数学", 59 }};//C++11支持括号初始化
score_umap["物理"] = 100;
score_umap.insert(pair<string, int>("英语", 66));
for(auto it = score_umap.begin(); it != score_umap.end(); ++it)
cout << it->first << "," << it->second << endl;
//find
auto it = score_umap.find("英语");//find()返回一个指向2的迭代器
if (it != score_umap.end())
cout << endl<< it->first << "," << it->second << endl;
}
int main()
{
unordered_map_test();
return 0;
}
C++ unordered_map的使用
最新推荐文章于 2024-09-11 19:54:50 发布