#include<iostream>
#include<unordered_map>
using namespace std;
int main(){
unordered_map<int,string> unomap;//创建一个key为string类型,value为int类型的unordered_map
int key = 4;
string value = "123";
unomap = {{1,"2"},{2,"3"}};
unomap.emplace(key,value);
unomap.emplace(7, "456");//也可以直接写上key和value的值
cout<<unomap[7]<<endl;
for(auto x: unomap){
cout<<x.first<<" "<<x.second<<endl;
}
for(auto x: unomap){
cout<<unomap[x.first]<<endl;
}
return 0;
}
hash表用法,记得几个关键的,
引入
#include <unordered_map>
定义
unordered_map<int, string> unomap;
引入的时候可以定义初始化,例如
unordered_map<int,string> unomap ={{1,"dd"},{2,"ssss"}};
打印查找:
for(auto x: unomap){
cout<<unomap[x.first]<<endl;
cout<<x.second<<endl;//以value值输出
}
myMap.insert(pair<int, string>(3,"陈二"));//插值用pair的形式
myMap.insert(pair<int,string>(3,"陈二"));//以pair的形式插入
myMap[2] = "zhangsan";//类比python字典,有加替换,value,没有直接创建一个出来。