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

c/c++ 标准库 map set 大锅炖

时间:2018-09-22 23:47:51      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:string   需要   自己的   gre   c++   出现   test   .com   pac   

标准库 map set 大锅炖

一,关联容器有哪些

按关键字有序保存元素
map 保存key和value
set 只保存key
mulutimap key可以重复出现
multiset key可以重复出现
无序集合
unordered_map 用哈希函数做成的map
unordered_set 用哈希函数做成的set
unordered_mulutimap key可以重复出现
unordered_multiset key可以重复出现

二,关联容器的类型别名

类型别名 功能描述
key_type 关键字(key)的类型
value_type set的话,和key_type一样;map的话,是个pair
mapped_type 只适用于map,值(value)的类型
set<string>::value_type v1("aa");//v1是string类型                             
set<string>::key_type v2("bb");//v2是string类型                               
map<string, int >::value_type v3{"aa",12};//v3是pair<const string,int>类型    
map<string, int>::key_type v4("cc");//v4是string类型                          
//只有map才有mapped_type                                               
map<string, int>::mapped_type v5(100);//v5是int类型

三,关联容器的迭代器

当解一个关联容器的迭代器,会得到一个类型为容器的value_type的值的引用。

  • 解一个map的迭代器,得到的是pair,key是const的,不可以更改。
  • 解一个set的迭代器,得到的是key也是const的,不可以更改。
map<string, int> cnt{{"aa",1}, {"bb",2}};
auto map_it = cnt.begin();
//map_it->first = "new key";//错误,first为const                              
++map_it->second;
cout << map_it->second << endl;//2                                            
set<int> iset{1,2,2,3,3,5};
set<int>::iterator set_it = iset.begin();
//*set_it = 10;//错误,不可以改变set的key

四,遍历关联容器

当使用迭代器遍历map,multimap,set,multiset时,迭代器按关键字升序遍历元素。

map<string, int> cnt{{"dd",1}, {"cc",2},{"aa", 10}};
auto map_it = cnt.cbegin();
while(map_it != cnt.cend()){
  cout << map_it->first << ":" << map_it->second << endl;
  ++map_it;
}
for(auto &s : cnt){
  cout << s.first << ":" << s.second << endl;
}

五,对关联容器使用泛型算法

  • 通常不对关联容器使用泛型算法,因为关联容器有key为const特性,很多算法不适用
  • 关联容器可用于只读算法,但是这些算法都需要搜索序列,比如find算法。但是关联容器有关键字,所以还莫不如用关联容器自己的find成员方法来的效率高。
  • 实际中,一般对关联容器,使用copy算法比较多
multiset<string> c{"aa","aa","dd","cc"};
vector<string> v{"ee","ff"};
//copy(v.begin(),v.end(), inserter(c, c.end()));//OK                          
//copy(v.begin(),v.end(), back_inserter(c));//NG,关联容器没有push_back       
//copy(c.begin(),c.end(), inserter(v, v.end()));//OK,并且c的begin到end时拍好序的                                                                            
copy(c.begin(),c.end(), back_inserter(v));//OK
for(auto &s : c){
  cout << s << " ";
}
cout << endl;
for(auto &s : v){
  cout << s << " ";
}
cout << endl;

小例子索引

代码块 功能描述
test1 关联容器的别名
test2 解关联容器的迭代器
test3 遍历关联容器
test4 对关联容器通用算法

小例子:

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

int main(){
  //test1 关联容器的别名                                                        
  /*                                                                            
  set<string>::value_type v1("aa");//v1是string类型                             
  set<string>::key_type v2("bb");//v2是string类型                               
  map<string, int >::value_type v3{"aa",12};//v3是pair<const string,int>类型    
  map<string, int>::key_type v4("cc");//v4是string类型                          
  //只有map才有mapped_type                                                      
  map<string, int>::mapped_type v5(100);//v5是int类型                           
  */

  //test2 解关联容器的迭代器                                                    
  /*                                                                            
  map<string, int> cnt{{"aa",1}, {"bb",2}};                                     
  auto map_it = cnt.begin();                                                    
  //map_it->first = "new key";//错误,first为const                              
  ++map_it->second;                                                             
  cout << map_it->second << endl;//2                                            
  set<int> iset{1,2,2,3,3,5};                                                   
  set<int>::iterator set_it = iset.begin();                                     
  //*set_it = 10;//错误,不可以改变set的key                                     
  */

  //test3 遍历关联容器                                                          
  /*                                                                            
  map<string, int> cnt{{"dd",1}, {"cc",2},{"aa", 10}};                          
  map<string, int>::const_iterator  map_it = cnt.cbegin();                      
  while(map_it != cnt.cend()){                                                  
    cout << map_it->first << ":" << map_it->second << endl;                     
    ++map_it;                                                                   
  }                                                                             
  for(auto &s : cnt){                                                           
    cout << s.first << ":" << s.second << endl;                                 
  }                                                                             
  auto it = cnt.begin();                                                        
  pair<string, int> p1 = *it;                                                   
  */

  //test4 关联容器的通用算法                                                    
  multiset<string> c{"aa","aa","dd","cc"};
  vector<string> v{"ee","ff"};
  //copy(v.begin(),v.end(), inserter(c, c.end()));//OK                          
  //copy(v.begin(),v.end(), back_inserter(c));//NG,关联容器没有push_back       
  //copy(c.begin(),c.end(), inserter(v, v.end()));//OK,并且c的begin到end时拍好\序的                                                                            
  copy(c.begin(),c.end(), back_inserter(v));
  for(auto &s : c){
    cout << s << " ";
  }
  cout << endl;
  for(auto &s : v){
    cout << s << " ";
  }
  cout << endl;
  return 0;
}

github完整代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ 标准库 map set 大锅炖

标签:string   需要   自己的   gre   c++   出现   test   .com   pac   

原文地址:https://www.cnblogs.com/xiaoshiwang/p/9691584.html

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