STL——map/unordered_map基礎用法

map /multimap


 map是STL裏重要容器之一。python

它的特性總結來說就是:全部元素都會根據元素的鍵值key自動排序(也可根據自定義的仿函數進行自定義排序),其中的每一個元素都是<key, value>的鍵值對,map中不容許有鍵值相同的元素,ios

所以map中元素的鍵值key不能修改,可是能夠經過key修改與其對應的value。若是必定要修改與value對應的鍵值key,可將已存在的key刪除掉,而後從新插入。c++

定義原型:編程

它做用應用場景可用做 ①字典    ②統計次數app

 

 

相關操做編程語言


(1)插入操做函數

方式有3種性能

 注:typedef pair<const Key, T> value_type;測試

 

經過插入新元素來擴展容器,經過插入元素的數量有效地增長容器容量。

因爲映射中的元素鍵是惟一的,所以插入操做將檢查每一個插入的元素是否具備與容器中已有元素相同的鍵,若是是,則不插入該元素,並將迭代器返回給此現有元素若是函數返回一個值)。

對於容許重複元素的相似容器,請參閱multimap。

在map中插入元素的另外一種方法是使用成員函數map :: operator []。

在容器內部,map容器按照其比較對象指定的標準,經過鍵將全部元素進行排序。這些元素老是按照這個順序插入到相應的位置。

this

返回值:

1.單個元素版本(1)返回一個pair,其成員pair :: first被設置爲一個迭代器,指向新插入的元素或映射中具備等效鍵的元素。若是插入了新元素,則將pair中pair :: second元素設置爲true;若是已存在相同的鍵,則將該元素設置爲false

2.帶有提示(2)的版本返回一個迭代器,指向新插入的元素或映射中已經具備相同鍵的元素。 成員類型迭代器是指向元素的雙向迭代器類型

 1  /*make_pair內斂函數 返回一個pair對象*/
 2  //template<class K, class V>  3  //inline pair<K, V> make_pair(const K& k, const V& v)  4  //{  5  // return pair<K, V>( k,v);  6  //} 
 7  void test_map_insert( )  8  {  9      //實現一個字典
10      typedef map<string, string>::iterator MyIterator; 11      map<string, string> direct; 12      direct.insert( pair<string, string>("insert", "插入" )); 13      direct.insert( pair<string, string>("sort", "排序" )); 14      direct.insert( make_pair("apple", "蘋果" )); 15      direct.insert( make_pair("insert", "插入" )); 16      direct.insert( make_pair("insert", "插入" )); 17      direct.insert( make_pair("sort", "排序" )); 18  
19      //(1)pair<iterator, bool> insert( const value_type& V); 
20      pair<MyIterator, bool> ret = direct.insert(make_pair("apple", "蘋果")); 21      if(ret.second == false)  //插入元素已經存在,將second置爲false
22  { 23          cout<<" 'apple' is inserted!"; 24          //first被設置爲一個迭代器,指向新插入的元素或映射中具備等效鍵的元素
25          cout<< "with value of "<<ret.first->second<<endl; 26  } 27      //(2)指定iterator插入
28      direct.insert(direct.begin( ), make_pair("os1", "操做系統1")); 29      direct.insert(direct.begin( ), make_pair("os2", "操做系統2")); //os2仍在os1後面 30  
31      //(3)範圍插入
32      map<string, string> another; 33  another.insert(direct.begin( ), direct.end( )); 34  
35      //遍歷(和set方式相似)
36      MyIterator it = direct.begin( ); 37      while( it != direct.end( )) 38  { 39          cout<<it->first<<": "<<it->second<<endl; 40          ++it; 41  } 42  }

 

 

(2)operator[]接口

原型以下:

map重載了「[]」運算符。重載的運算符「[]」實質上調用了前面中版本(1)的insert接口,它利用了insert的返回值(一個pair<iterator, bool>類型),最後返回pair中的迭代器所指元素value值的引用。如此,即可經過「[]」 來進行map的插入操做,與此同時,還可對新插入的元素(或插入元素在map已經存在的元素)的value值進行修改。重載[]具體實現以下:

(*((this->insert(make_pair(k,mapped_type()))).first)).second

 對它進行大體解析後,可將其修改成:

 template<class K, class V> typedef map<K, V>::iterator MyIterator; mapped_type& operator[](const K& k) { //mapped_type是V值(value)的默認值,value爲int的話則默認爲0
     pair<MyIterator, bool> ret = this->insert(make_pair(k, mapped_type())); return ret.first->second;  //或者 *(ret.first ).second; ret.first是MyIterator迭代器,最後返回迭代器所指元素的second值(也便是value)的引用。
 }

例子

// accessing mapped values
#include <iostream> #include <map> #include <string>

int main () { std::map<char,std::string> mymap; mymap['a']="an element"; mymap['b']="another element"; mymap['c']=mymap['b'];  //插入key爲‘c’的元素,隨後將其對應value值修改。 //key爲'a'的元素已經插入,此時返回‘a’所對應value的值
  std::cout << "mymap['a'] is " << mymap['a'] << '\n'; //key爲'b'的元素已經插入,此時返回‘b’所對應value的值
  std::cout << "mymap['b'] is " << mymap['b'] << '\n'; //key爲'c'的元素已經插入,此時返回‘c’所對應value的值
  std::cout << "mymap['c'] is " << mymap['c'] << '\n'; //直接插入key爲‘a’的元素,元素對應value值爲默認的「」
  std::cout << "mymap['d'] is " << mymap['d'] << '\n'; std::cout << "mymap now contains " << mymap.size() << " elements.\n"; return 0; } //結果 /*mymap['a'] is an element mymap['b'] is another element mymap['c'] is another element mymap['d'] is mymap now contains 4 elements*/

 值得注意的是,經過"[]"操做,若能確保查找元素在map中存在,還能夠用它進行查找操做。由於在執行「[]」操做的過程當中,插入失敗會返回與查找元素擁有相同key值的一個iterator。

 

(3)按自定義順序排序

 一般map對傳入的元素,默認是按元素中key值進行排序(即前面定義的Less<Key>),經過前面的map原型定義不難看出它一樣支持按自定義的順序進行比較排序。例如咱們自定義的Stu的對象,就可按對象中的年齡來進行排序:

 struct Stu { string name; int age; Stu(const string& na="", int a = 0) :name( na), age(a) { }; }; //定置一個仿函數
 struct compare { bool operator( )(const Stu& l, const Stu& r) { return l.age < r.age;} }; void test_insert_compare() {//按傳入Stu對象的年齡排序
     multimap<Stu, int, compare>sortmap; Stu s1("jack", 18); sortmap.insert(make_pair(s1, 1)); Stu s2( "mike", 20); sortmap.insert(make_pair(s2, 1)); Stu s3( "zede", 19); sortmap.insert(make_pair(s3, 1)); map<Stu,int, compare>::iterator it = sortmap.begin( ); while( it != sortmap.end( )) { cout<<it->first.name<<": "<<it->first.age<<" --"<<it->second<<endl; ++it; } //結果: //jack: 18 --1 //zede: 19 --1 //mike: 20 --1
 }

 

小應用:據出現次數,統計前K項語言

 


 

 //定製一個仿函數
 typedef map<string, int>::iterator CountIte; struct compare { bool operator()(CountIte lhs, CountIte rhs){ return lhs->second > rhs->second; } }; void get_topK_gramar(const vector<string>& v, int k) { //統計vector中各類相同key出現的次數
     map<string, int> countMap; for( int i =0; i< v.size( ); ++i) { // map<string, int>::iterator it = countMap.find(v[ i]); // if(it != countMap.end( ))//countmap中存在v[ i] // ++it->second; // else // countMap.insert( make_pair(v[i], 1);
         countMap[v[i]]++; } //定置仿函數,以每種編程語言出現次數進行排序 //注意:不能用set來排序,由於它會去重,即其會將具備相同value值的某種語言過濾掉
     multiset<CountIte, compare> sortSet; CountIte cit = countMap.begin( ); while( cit != countMap.end( )) { sortSet.insert(cit); ++cit; }
 multiset<CountIte, compare>::iterator it1 = sortSet.begin( ); for(; it1 != sortSet.end( ); ++it1) { if( k--) cout<<(*it1)->first<<":"<<(*it1)->second<<endl; } } void test_map_question( ) { vector<string> v; v.push_back("python" ); v.push_back("PHP" ); v.push_back("PHP" ); v.push_back("PHP" ); v.push_back("PHP" ); v.push_back("Java" ); v.push_back("PHP" ); v.push_back("C/C++" ); v.push_back("C/C++" ); v.push_back("python" ); v.push_back("Java" ); v.push_back("Java" ); //統計語言次數,或者前K種語言
     get_topK_gramar(v, 3); }

結果:

 

 

 multimap


multimap和map的惟一差異是map中key必須是惟一的,而multimap中的key是能夠重複的。因爲不用再判斷是否插入了相同key的元素,因此multimap的單個元素版本的insert的返回值再也不是一個pair, 而是一個iterator。也正是如此,因此multimap也再也不提供operator[]接口。

multimap和map的其它用法基本相似。

 

 

unordered_map/unordered_multimap


在C++11中有新出4個關聯式容器:unordered_map/unordered_set/unordered_multimap/unordered_multiset。

這4個關聯式容器與map/multimap/set/multiset功能基本相似,最主要就是底層結構不一樣,使用場景不容。

若是須要獲得一個有序序列,使用紅黑樹系列的關聯式容器,若是須要更高的查詢效率,使用以哈希表爲底層的關聯式容器。 

此處只列舉unordered_map,其它用法相似可自行查閱 可參考cplusplus

 unordered_map底層實現是用哈希桶實現的:

定義原型

在cplusplus的解釋:

無序映射是關聯容器,用於存儲由鍵值和映射值組合而成的元素,並容許基於鍵快速檢索各個元素。

在unordered_map中,鍵值一般用於惟一標識元素,而映射值是與該鍵關聯的內容的對象。鍵和映射值的類型可能不一樣。

在內部,unordered_map中的元素沒有按照它們的鍵值或映射值的任何順序排序,而是根據它們的散列值組織成桶以容許經過它們的鍵值直接快速訪問單個元素(具備常數平均時間複雜度)。

unordered_map容器比映射容器更快地經過它們的鍵來訪問各個元素,儘管它們經過其元素的子集進行範圍迭代一般效率較低。

無序映射實現直接訪問操做符(operator []),該操做符容許使用其鍵值做爲參數直接訪問映射值。

容器中的迭代器至少是前向迭代器。

關鍵詞:無序的 快速的檢索 達到的是更快的訪問 可是子集的範圍迭代效率低

 

相關操做


 1.插入遍歷...

 typedef unordered_map<string, double>::iterator MyIte; void test_unordered_map( ) { unordered_map<string, double> umap; umap.insert(make_pair("蘋果", 2.5)); umap.insert(make_pair("香蕉", 3.0)); umap.insert(make_pair("香蕉", 3.0)); umap.insert(make_pair("西瓜", 1.5)); umap.insert(make_pair("哈密瓜", 3.0)); umap["榴蓮"] = 4.0; MyIte it = umap.begin( ); while( it != umap.end( )) { cout<<it->first<<" :"<<it->second<<endl; ++it; } cout<<"桶數量:"<<umap.bucket_count( )<<endl; cout<<"負載因子:"<<umap.load_factor( )<<endl; //結果: //榴蓮 :4 //蘋果 :2.5 //哈密瓜 :3 //香蕉 :3 //西瓜 :1.5 //桶數量:11 //負載因子:0.454545 }

 

2.自定義比較

 struct Store
 {
     string name;
     string addr;
     Store(const string& na="", const string& ad= "")
         :name(na),addr( ad){ }
 
     bool operator==(const Store& s) const     //重載==支持等於比較
     {   
         return name == s.name && addr == s.addr; 
     }
 };
 struct hash_key    //定製返回哈希值的仿函數
 {
     //BKDRHash
     size_t operator()(const Store& s) const
     {
         size_t seed = 131; /* 31 131 1313 13131 131313 etc.. */
         size_t hash = 0;
         size_t i = 0;
         for( i = 0; i < s.name.size(); ++i)
         {
             hash = ( hash * seed)  + s.name[i];
         }
         return hash;
     }
 };
 
 typedef unordered_map<Store, int, hash_key>::iterator MyIte;
 void test_unordered_map( )
 {
     unordered_map<Store, int, hash_key> umap;
     Store s1("火鍋店", "重慶");
     Store s2("涼皮店", "西安");
     Store s3("烤鴨店", "北京");
 
     umap.insert(make_pair(s1, 1));
     umap.insert(make_pair(s2, 1));
     umap[s3] = 1;
 
     MyIte it = umap.begin( );
     while( it != umap.end( ))
     {
         cout<<it->first.name<<""<<it->second<<endl;
         ++it;
     }
 }

 

 

3. 性能測試

測試insert  比較map和unordered_map性能差別

 typedef unordered_map<int, int>::iterator MyIte; void test_unordered_map( ) { unordered_map<int, int> umap; map<int, int>mp; srand(time( NULL)); const int N = 100000; vector<int> a; for(int i=0; i< N; ++i) a.push_back(rand()%N); clock_t begin1,end1; begin1 = clock(); umap.rehash(100000);  //經過rehash設置哈希桶數量,進一步提升效率
     for(int i =0; i< N; ++i) umap[a[i]]; end1 = clock( ); clock_t begin2, end2; begin2 = clock( ); for( int i =0; i< N; ++i) mp[a[i]]; end2= clock( ); cout<<"負載因子:"<<umap.load_factor()<<" "<<"桶數:"<<umap.bucket_count( )<<endl; //統計運行的毫秒差
     cout<<"unordered_map:"<<(end1-begin1)/1000<<endl; cout<<"map:"<<(end2-begin2)/1000<<endl;//統計運行的毫秒差
 }
//結果 普通
tp@tp:~$ g++ -std=c++11 1.cc tp@tp:~$ ./a.out 負載因子:0.500463 桶數:126271 unordered_map:49 map:102

//rehash以後
負載因子:0.585883 桶數:107897 unordered_map:37 map:107

 

 

 

unordered_map 與 map之間差別比較(Linux平臺下)

·map底層爲紅黑樹查找大體爲logN的時間複雜度;unordered_map底層是閉散列的哈希桶,查找爲O(1),性能更優。

·調用insert操做,map相較於unordered_map操做慢,大體有2到3倍差別;可是map插入更加穩定

·unordered_map的erase操做會縮容,致使元素從新映射,下降性能。

·unordered_map要求傳入的數據可以進行大小比較,「==」關係比較;因此自定義數據須要定置hash_value仿函數同時重載operator==。

相關文章
相關標籤/搜索