博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 学习小程序之 map 的用法
阅读量:7098 次
发布时间:2019-06-28

本文共 2637 字,大约阅读时间需要 8 分钟。

1. map::at

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 int main(){ 7 map
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<
<<": "<
<<'\n';18 }19 20 return 0;21 }

 

2. make_pair example

1 // make_pair example 2 #include 
// std::pair 3 #include
// std::cout 4 5 int main () { 6 std::pair
foo; 7 std::pair
bar; 8 9 foo = std::make_pair (10,20);10 bar = std::make_pair (10.5,'A'); // ok: implicit conversion from pair
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 
3 #include
4 5 int main () 6 { 7 std::map
mymap; 8 9 mymap['b'] = 100;10 mymap['a'] = 200;11 mymap['c'] = 300;12 13 // show content:14 for (std::map
::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 
3 #include
4 using namespace std; 5 int main () 6 { 7 map
mymap; 8 9 // first insert function version (single parameter):10 mymap.insert ( pair
('a', 100) );11 mymap.insert ( pair
('z', 200) );12 13 pair
::iterator, bool> ret;14 ret = mymap.insert (pair
('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
::iterator it = mymap.begin();22 mymap.insert (it, pair
('b',300)); // max efficiency inserting23 mymap.insert (it, pair
('c',400)); // no max efficiency inserting24 25 //third insert function version (range insertion):26 map
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<
first<<"=>"<
second<<'\n';33 34 cout<<"anothermap contains: \n";35 for(it=anothermap.begin(); it!=anothermap.end();++it)36 cout<
first<<"=>"<
second<<'\n';37 38 return 0;39 }

 

转载地址:http://qchql.baihongyu.com/

你可能感兴趣的文章
Hadoop(一)Hadoop的介绍和安装前准备
查看>>
如何把百度网盘下载速度提高 100 倍,我推荐这个下载工具
查看>>
CentOS RabbitMQ安装
查看>>
小程序内置组件swiper,circular(衔接)使用小技巧
查看>>
JVM垃圾回收机制
查看>>
node结合swig渲染摸板实现前后端不分离
查看>>
聊聊springcloud的featuresEndpoint
查看>>
厉害了,蚂蚁金服!创造了中国自己的数据库OceanBase(下)
查看>>
require源码阅读
查看>>
事务 - Saga模式
查看>>
segmentfault APP改版之后超级不好用
查看>>
【GIT】常见使用GIT的出现的问题 - 进阶篇
查看>>
JavaScript之this对象详解
查看>>
一个菜鸟(老yin逼)教后端的你如何"一天"做好微信小程序
查看>>
vue项目实战(第二回)
查看>>
关于mockjs的使用
查看>>
nodejs之express小记
查看>>
译|调整JavaScript抽象的迭代方案
查看>>
字符串与数组的方法总结(包含ES6)
查看>>
git入门
查看>>