1. 程式人生 > >c/c++ 標準庫 map set 大鍋燉

c/c++ 標準庫 map set 大鍋燉

space 無序集合 string類 str using while key namespace 序列

標準庫 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 大鍋燉