C++哈希表用法

#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,没有直接创建一个出来。

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值