STL實踐與分析--multimap與multiset
引言:
map和set容器中,一個鍵只能對應一個執行個體,而multiset和multimap類型則允許一個鍵對應多個執行個體。
multimap與multiset類型與相應的單元素版本具有相同的標頭檔定義:分別是map與set標頭檔。
multimap/multiset類型與map/set的操作相同,只有一個例外:multimap不支援下標運算。因為在這類容器中,某個鍵可能對應多個值。因此,在使用multimap/multiset時,對於某個鍵,必須做好處理多個值的準備,而非只有單一的值。
一、元素的添加與刪除
前面所描述的insert操作和 erase操作同樣也適用於multimap以及 multiset容器。只是不同的是:由於鍵不要求是唯一的,因此每次調用insert總會添加一個元素。
multimap<string,string> authors; authors.insert(make_pair("Barth John","Sot-Weed Factor")); authors.insert(make_pair("Barth John","Sot-Weed Factor"));
帶有一個鍵參數的erase版本將刪除擁有該鍵的所有元素,並返回刪除的個數。而帶有一個或一對迭代器參數的版本只刪除指定的元素,並返回void類型:
multimap<string,string> authors; authors.insert(make_pair("Barth John","Sot-Weed Factor")); authors.insert(make_pair("Barth John","Sot-Weed Factor")); authors.insert(make_pair("Barth John","Sot-Weed Factor")); string searchStr("Barth John"); multimap<string,string>::size_type cnt = authors.erase(searchStr); cout << cnt << endl;//cnt == 3
二、在multimap和multiset中尋找元素
關聯容器nap和set的元素是按照順序儲存的,而multimap和multiset也一樣。因此,在multiset和multim容器中,如果某個鍵對應於多個執行個體,則這些執行個體在容器中必然相鄰存放。
因此,在遍曆multimap/multiset容器時,可以保證依次返回特定鍵所關聯的所有元素。
1、使用find和count操作
使用count函數可以求出某個鍵出現的次數,而find操作將返回一個迭代器,指向第一個擁有正在尋找的鍵的執行個體:
multimap<string,string> authors; string first,second; while (getline(cin,first)) { getline(cin,second); authors.insert(make_pair(first,second)); } string search_item("Alain de Botton"); typedef multimap<string,string>::size_type sz_type; sz_type entries = authors.count(search_item); multimap<string,string>::iterator iter = authors.find(search_item); for (sz_type cnt = 0;cnt != entries; ++cnt,++iter) { cout << iter -> second << endl; } cout << "First:\t\t\tSecond:" << endl; for (multimap<string,string>::iterator iter = authors.begin(); iter != authors.end(); ++iter) { cout << iter -> first << "\t\t" << iter -> second << endl; }
2、與眾不同的面向迭代器的解決方案
下表列出的這些操作適用於所有的關聯容器,也可用於普通的map和set容器,但是更常用於multiamp和multiset容器。所有的這些操作都需要一個鍵,並返回一個迭代器。
|
返回迭代器的關聯容器操作 |
|
m.lower_bound(k) |
返回一個迭代器,指向鍵不小於k的第一個元素 |
|
m.upper_bound(k) |
返回一個迭代器,指向鍵大於k的第一個元素 |
|
m.equal_range(k) |
他的first成員等價於m.lower_bound(k),
second成員等價於m.upper_bound(k) |
在同一個鍵上調用lower_bound函數和upper_bound函數將產生一個迭代器範圍,指出該鍵所關聯的所有元素。如果該鍵在容器中存在,則會獲得兩個不同的迭代器:lower_bound返回的迭代器指向該鍵關聯的第一個執行個體,而upper_bound返回的迭代器則指向最後一個執行個體的下一位置。如果該鍵不在multimap中,這兩個操作將返回同一個迭代器,都指向同一個元素或者同時指向multimap超出末端的位置。
typedef multimap<string,string>::iterator authors_it; string searchVal = "Barth John"; authors_it beg = authors.lower_bound(searchVal), end = authors.upper_bound(searchVal); while (beg != end) { cout << beg -> second << endl; ++ beg; }
這兩個操作不會說明鍵的存在,其關鍵之處在於傳回值給出了迭代器範圍。
3、equal_range函數
可用通過調用equal_range函數來取代調用upper_bound和lower_bound函數。equal_range函數返回儲存一對迭代器的pair對象。如果該值存在,則pair對象中的第一個迭代器指向該鍵關聯的第一個執行個體,第二個迭代器指向該鍵關聯的最後一個執行個體的下一位置。如果找不到匹配的元素,則pair對象中的兩個迭代器都將指向此鍵應該插入的位置。
pair<authors_it,authors_it> pos = authors.equal_range(searchVal); while (pos.first != pos.second) { cout << pos.first -> second << endl; ++ pos.first; }
//附:以上兩個程式的未用typedef版本 string searchVal = "Barth John"; multimap<string,string>::iterator beg = authors.lower_bound(searchVal), end = authors.upper_bound(searchVal); cout << "First:\t\t\tSecond:" << endl; while (beg != end) { cout << beg -> first << "\t\t" << beg -> second << endl; ++ beg; } cout << endl; pair<multimap<string,string>::iterator,multimap<string,string>::iterator> pos = authors.equal_range(searchVal); while (pos.first != pos.second) { cout << pos.first -> first << "\t\t" << pos.first -> second << endl; ++ pos.first; }
//P325 習題10.26int main(){ multimap<string,string> authors; ifstream inFile("input"); string first,second; while (getline(inFile,first)) { getline(inFile,second); authors.insert(make_pair(first,second)); } string eraseVal("Barth John"); if (authors.find(eraseVal) != authors.end()) { multimap<string,string>::size_type cnt = authors.erase(eraseVal); cout << "Have erased " << cnt << " books" << endl; } else { cout << "Have erased 0 book" << endl; }}
//習題10.27 multimap<string,string> authors; ifstream inFile("input"); string first,second; while (getline(inFile,first)) { getline(inFile,second); authors.insert(make_pair(first,second)); } string eraseVal("Barth John"); pair<multimap<string,string>::iterator,multimap<string,string>::iterator> pos = authors.equal_range(eraseVal); if (pos.first != pos.second) { authors.erase(pos.first,pos.second); } else { cout << "Have not found eraseVal " << eraseVal << endl; } cout << "First:\t\tSecond:" << endl; for (multimap<string,string>::iterator iter = authors.begin(); iter != authors.end(); ++iter) { cout << iter -> first << "\t" << iter -> second << endl; }
//習題10.28 這節中非常好的習題,值得仔細品讀int main(){ multimap<string,string> authors; string author,book; ifstream inFile("input"); while (getline(inFile,author)) { getline(inFile,book); authors.insert(make_pair(author,book)); } multimap<string,string>::iterator iter = authors.begin(); if (iter == authors.end()) { cout << "map is empty" << endl; return 0; } /* for (multimap<string,string>::iterator it = authors.begin(); it != authors.end(); ++it) { cout << it -> first << ' ' << it -> second << endl; } */ string currAuthor,preAuthor; do { currAuthor = iter -> first; if (preAuthor.empty() || currAuthor[0] != preAuthor[0]) { cout << "Author Names Beginning with " << currAuthor[0] << " :" << endl; } cout << "\t" << currAuthor; pair<multimap<string,string>::iterator,multimap<string,string>::iterator> pos = authors.equal_range(currAuthor); while (pos.first != pos.second) { cout << ',' << pos.first -> second; ++ (pos.first); } cout << endl; iter = pos.second; preAuthor = currAuthor; } while (iter != authors.end());}
獲得“專家”之後的第一篇部落格,紀念一下O(∩_∩)O哈哈~,這也是最後一篇專門講述容器的部落格了,明天,開始演算法之旅!