码迷,mamicode.com
首页 > 编程语言 > 详细

c++学习笔记(八)- map

时间:2018-02-13 17:54:52      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:++   namespace   iter   tor   笔记   log   输出   返回   bool   

map<key, value>是按key排好序的,key不可以重复。

1. map.lower_bound():按key查找,如果查找的key存在,返回该位置,如果不存在返回大于所查找值的最小key所在位置

 1 #include <iostream>
 2 #include <map>
 3 using namespace std;
 4 
 5 int main ()
 6 {
 7   std::map<char,int> mymap;
 8   std::map<char,int>::iterator itlow,itup;
 9 
10   mymap[a]=20;
11   mymap[b]=40;
12   mymap[b]=50;  //key值不重复,会把b的value更新为50    
13   mymap[c]=80;  //value的值可以重复
14   mymap[d]=80;
15   mymap[e]=100;
16 
17   for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
18     std::cout << it->first << " => " << it->second << \n;
19   cout<<endl;
20                                         //这段程序其实并不能成功插入,因为key=b已经存在了,value也不会更新
21   itlow=mymap.lower_bound (b);                     //如果键值b存在,itlow指向b,否则指向比b大的最小key所在位置
22   mymap.insert(itlow,std::map<char,int>::value_type(b,40));   //向itlow指向的位置插入map<‘b‘,40>
23 
24   for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
25     std::cout << it->first << " => " << it->second << \n;
26   cout<<endl;
27 
28   char c=a;
29   while(1)
30   { //输入一个字符并插入
31     cin>>c;  
32     itlow = mymap.lower_bound(c);
33     if(itlow != mymap.end())
34       cout<<"insert "<<c<<" to position: "<<itlow->first<<" , "<<itlow->second<<\n;
35     mymap.insert(itlow, map<char,int>::value_type(c, 160+c));
36     for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
37       std::cout << it->first << " => " << it->second << \n;
38     cout<<endl;
39   }
42   return 0;
43 }

输出结果:

 1 a => 20
 2 b => 50
 3 c => 80
 4 d => 80
 5 e => 100
 6 
 7 a => 20
 8 b => 50
 9 c => 80
10 d => 80
11 e => 100
12 
13 d  //并没有成功插入,d的value还是原来的80
14 insert d to position: d , 80
15 a => 20
16 b => 50
17 c => 80
18 d => 80
19 e => 100
20 
21 z //插入位置在map.end
22 a => 20
23 b => 50
24 c => 80
25 d => 80
26 e => 100
27 z => 282
28 
29 x
30 insert x to position: z , 282
31 a => 20
32 b => 50
33 c => 80
34 d => 80
35 e => 100
36 x => 280
37 z => 282

2. map.key_comp(),只有第一个参数小于第二个才返回true。

    函数返回值是bool型,输入是两个key值,key_comp()(a,b),当a<b时返回1,否则返回0。

 1 #include <iostream>
 2 #include <map>
 3 using namespace std;
 4 int main ()
 5 {
 6   std::map<char,int> mymap;
 7 
 8   mymap[a] = 20;
 9   mymap[c] = 120;
10   mymap[x]=101;
11   mymap[y]=203;
12   mymap[z]=103;
13 
14   std::cout << "mymap contains:\n";
15   map<char,int>::iterator it = mymap.begin();
16   for( it = mymap.begin();it!=mymap.end();it++)
17     cout<<it->first<<" => "<<it->second<<\n;
18   cout<<endl;
19 
20   map<char,int>::iterator pit = mymap.begin();
21   pit++;
22   pit++;  //pit指向第三个元素map<x,101>
23 
24   it = mymap.begin();
25   for( it = mymap.begin();it!=mymap.end();it++)
26   {
27      cout<<"key comp result: "<<mymap.key_comp()(it->first, pit->first)<<endl;
28   }
29   cout<<endl;
30   return 0;
31 }

结果:

 1 mymap contains:
 2 a => 20
 3 c => 120
 4 x => 101
 5 y => 203
 6 z => 103
 7 
 8 key comp result: 1
 9 key comp result: 1
10 key comp result: 0 //此时it=pit
11 key comp result: 0
12 key comp result: 0

 

c++学习笔记(八)- map

标签:++   namespace   iter   tor   笔记   log   输出   返回   bool   

原文地址:https://www.cnblogs.com/zhengmeisong/p/8446883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!