网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

C++语言STL库中unordered_map容器的基本用法

时间:2024-10-13 21:45:31

1、头文件的声明。头文件的话,这里我们可以声明一种万能头文件,这样就能直接使用unordered_map这一容器了。#include<bits/stdc++.h>

C++语言STL库中unordered_map容器的基本用法

3、元素插入可以使用两种方法网unordered_map中插入数值。第一种:使用操作符[]直接插入例如:umap["a1"]=2;umap["a3"]=7;umap["a2"]=5;

C++语言STL库中unordered_map容器的基本用法

5、数撕良滤儆值搜索使用find方法进行数值搜索。例如:string key="a3"; if (umap.find(key)==umap.end()) cout<&造婷用痃lt;key<<"Not found "<<endl; else cout<<"found "<<key<<endl;

C++语言STL库中unordered_map容器的基本用法

7、最后一步提供以上几部的代码提供参考。#include <iostream>#include媪青怍牙<bits/stdc++.h>using namespace std;int main(){ unordered_map<string,int> umap; //insert value by operator [] umap["a1"]=2; umap["a3"]=7; umap["a2"]=5; //insert value by insert function umap.insert(make_pair("e",7)); string key="a3"; if (umap.find(key)==umap.end()) cout<<key<<"Not found "<<endl; else cout<<"found "<<key<<endl; unordered_map<string,int>::iterator i; cout<<"All value"<<endl; for (i=umap.begin();i!=umap.end();i++) cout<<i->first<<" "<<i->second<<endl; return 0;}

C++语言STL库中unordered_map容器的基本用法
© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com