0
点赞
收藏
分享

微信扫一扫

map

夹胡碰 2022-05-25 阅读 59
1 #include <iostream>
2 #include <cstdio>
3 #include <map>
4 using namespace std;
5 int main()
6 {
7 map<char,int> mp;
8 map<char,int> ::iterator it;
9 mp.insert({'a',1});
10 mp.insert({'b',2});
11 mp.insert({'c',3});
12 // mp['a']=1;
13 // mp['b']=2;
14 // mp['c']=3;
15 cout<<mp.size()<<endl;
16 for(it=mp.begin();it!=mp.end();it++){
17 cout<<it->first<<" "<<it->second<<endl;
18 }
19 // 3
20 // a 1
21 // b 2
22 // c 3
23 return 0;
24 }


1 #include <cstdio>
2 #include <iostream>
3 #include <queue>
4 #include <set>
5 #include <string>
6 #include <map>
7 using namespace std;
8 int main()
9 {
10 //声明(int为键,const char*为值)
11 map<int, const char*> m;
12
13 //插入元素
14 m.insert(make_pair(1,"one"));
15 m.insert(make_pair(10,"ten"));
16 m[100]="hundred";//其他写法
17
18 //查找元素
19 map<int, const char*> ::iterator ite;
20 ite=m.find(1);
21 cout<<ite->second<<endl;
22
23 ite=m.find(2);
24 if(ite==m.end()) cout<<"Not found"<<endl;
25 else cout<<ite->second<<endl;
26
27 cout<<m[10]<<endl;//其他写法
28
29 //删除元素
30 m.erase(10);
31 //遍历一遍所有的元素
32 for(ite=m.begin();ite!=m.end();ite++) cout<<ite->first<<" "<<ite->second<<endl;
33
34 return 0;
35 }





举报

相关推荐

0 条评论