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

简介: 1. map::at 1 #include 2 #include 3 #include 4 using namespace std; 5 6 int main(){ 7 map mymap = { 8 {"alpha", 0}, ...

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 }

 

相关文章
|
3天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
17天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
19 2
|
3天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
1天前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
2天前
|
C语言 C++
c++的学习之路:4、入门(3)
c++的学习之路:4、入门(3)
17 0
|
2天前
|
编译器 C++
c++的学习之路:23、多态(2)
c++的学习之路:23、多态(2)
17 0
|
5天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的英语学习交流平台的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的英语学习交流平台的详细设计和实现
21 2
|
5天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的移动学习平台的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的移动学习平台的详细设计和实现
30 1
|
17天前
|
程序员 C++
C++语言模板学习应用案例
C++模板实现通用代码,以适应多种数据类型。示例展示了一个计算两数之和的模板函数`add&lt;T&gt;`,可处理整数和浮点数。在`main`函数中,展示了对`add`模板的调用,分别计算整数和浮点数的和,输出结果。
12 2
|
22天前
|
人工智能 安全 机器人
【C++】const_cast基本用法(详细讲解)
【C++】const_cast基本用法(详细讲解)

热门文章

最新文章