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

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

-----------2018.03.09--------------------

db_feature.insert(std::pair<int, std::vector<std::vector<unsigned char>> >(id,feature) );   //如果id已经存在,不会成功插入
db_feature[id] = feature;                    //如果id已经存在,修改key=id的value

 ----------2019.01.08---------------------

map最后一个元素访问并删除

1. 直接调stl函数

*(map.rbegin());   //map最后一个元素

访问并删除:

key = m_right.rbegin()->first;        //map倒叙第一个元素(也就是正序最后一个)
value = m_right.rbegin()->second;
m_right.erase(key);                  //map的key是唯一的,可以按key删除
//m_right.erase(m_right.rbegin());      //这样删除会报错        

注意这个rbegin是一个反向迭代器:

// map<TreeNode*, int>::iterator it = m_right.rbegin();       //这样的索引是错的    
map<TreeNode*, int>::reverse_iterator it = m_right.rbegin();            

 这也解释了为什么不能用rbegin()删除元素,因为map.erase不支持反向迭代器的删除:

2. 使用end再自减迭代器

auto it = m_right.end();      //it指向最后一个元素的下一个地址
it--;                         //it指向最后一个元素
root = it->first;
s = it->second;
m_right.erase(it);            //删除迭代器指向的元素

关于map.end(), c++参考里描述是指向“past-the-end element”,stackoverflow对这个past-the-end的解释

 ---------------2019.02.27----------------------------

map按key长度排序、倒排等

map按value排序

posted @ 2018-02-13 16:22  tszs_song  阅读(402)  评论(0编辑  收藏  举报