C++ 学习小程序之 map 的用法

本文介绍了C++标准模板库中Map容器的多种使用方法,包括at方法的使用、make_pair函数创建对、迭代器遍历map以及insert方法的三种形式。通过具体示例展示了如何高效地管理和操作键值对数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. map::at

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 using namespace std;
 5 
 6 int main(){
 7     map<string, int> mymap = {
 8         {"alpha", 0},
 9         {"beta", 0},
10         {"gamma", 0}};
11 
12     mymap.at("alpha") = 10;
13     mymap.at("beta") = 20;
14     mymap.at("gamma") = 30;
15 
16     for (auto& x:mymap){
17         cout<<x.first<<": "<<x.second<<'\n';
18     }
19 
20     return 0;
21 }

 

2. make_pair example

 1 // make_pair example
 2 #include <utility>      // std::pair
 3 #include <iostream>     // std::cout
 4 
 5 int main () {
 6   std::pair <int,int> foo;
 7   std::pair <int,int> bar;
 8 
 9   foo = std::make_pair (10,20);
10   bar = std::make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>
11 
12   std::cout << "foo: " << foo.first << ", " << foo.second << '\n';
13   std::cout << "bar: " << bar.first << ", " << bar.second << '\n';
14 
15   return 0;
16 }

 

3. map::begin/end

 1 // map::begin/end
 2 #include <iostream>
 3 #include <map>
 4 
 5 int main ()
 6 {
 7   std::map<char,int> mymap;
 8 
 9   mymap['b'] = 100;
10   mymap['a'] = 200;
11   mymap['c'] = 300;
12 
13   // show content:
14   for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
15     std::cout << it->first << " => " << it->second << '\n';
16 
17   return 0;
18 }

4.   map::insert(C++98)

 1 // map::insert(C++98)
 2 #include <iostream>
 3 #include <map>
 4 using namespace std;
 5 int main ()
 6 {
 7     map<char,int> mymap;
 8 
 9     // first insert function version (single parameter):
10     mymap.insert ( pair<char,int>('a', 100) );
11     mymap.insert ( pair<char,int>('z', 200) );
12 
13     pair<map<char, int>::iterator, bool> ret;
14     ret = mymap.insert (pair<char,int>('z',500));
15     if (ret.second == false){
16         cout<<"element 'z' already existed";
17         cout<<"with a value of " << ret.first->second << '\n';
18     }
19 
20     //second insert function version (with hint position):
21     map<char, int>::iterator it = mymap.begin();
22     mymap.insert (it, pair<char, int>('b',300)); // max efficiency inserting
23     mymap.insert (it, pair<char, int>('c',400)); // no max efficiency inserting
24 
25     //third insert function version (range insertion):
26     map<char,int> anothermap;
27     anothermap.insert(mymap.begin(),mymap.find('c'));
28 
29     // showing contents:
30     cout<<"mymap contains: \n";
31     for (it = mymap.begin(); it!= mymap.end(); ++it)
32         cout<<it->first<<"=>"<<it->second<<'\n';
33 
34     cout<<"anothermap contains: \n";
35     for(it=anothermap.begin(); it!=anothermap.end();++it)
36         cout<<it->first<<"=>"<<it->second<<'\n';
37 
38     return 0;
39 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值