首先看一下set與map的介紹:
set關(guān)聯(lián)式容器,set作為一個容器也是用來存儲同一數(shù)據(jù)類型的數(shù)據(jù)類型,并且能從一個數(shù)據(jù)集合中取出數(shù)據(jù),在set中每個元素的值都唯一,而且系統(tǒng)能根據(jù)元素的值自動進(jìn)行排序。應(yīng)該注意的是set中數(shù)元素的值不能直接被改變。C++ STL中標(biāo)準(zhǔn)關(guān)聯(lián)容器set, multiset, map, multimap內(nèi)部采用的就是一種非常高效的平衡檢索二叉樹:紅黑樹,也成為RB樹(Red-Black Tree)。RB樹的統(tǒng)計性能要好于一般平衡二叉樹,所以被STL選擇作為了關(guān)聯(lián)容器的內(nèi)部結(jié)構(gòu)。
map是STL的一個關(guān)聯(lián)容器,它提供一對一(其中第一個可以稱為關(guān)鍵字,每個關(guān)鍵字只能在map中出現(xiàn)一次,第二個 可能稱為該關(guān)鍵字的值)的數(shù)據(jù)處理能力,由于這個特性,它完成有可能在我們處理一對一數(shù)據(jù)的時候,在編程上提供快速通道。這里說下map內(nèi)部數(shù)據(jù)的組織,map內(nèi)部自建一顆紅黑樹(一種非嚴(yán)格意義上的平衡二叉樹),這顆樹具有對數(shù)據(jù)自動排序的功能,所以在map內(nèi)部所有的數(shù)據(jù)都是有序的。
作為STL容器,他們有著相同的操作:插入,刪除,查找等。 set與map也是存在著區(qū)別的: set的結(jié)點是一個數(shù)據(jù),set(集合)——包含了經(jīng)過排序了的數(shù)據(jù),這些數(shù)據(jù)的(value)必須是唯一的。 map的結(jié)點是一對數(shù)據(jù),map(映射)——經(jīng)過排序了的二元組的集合,map中的每個元素都是由兩個值組成,其中的key(鍵值,一個map中的鍵值必須是唯一的)是在排序或搜索時使用,它的值可以在容器中重新獲取;而另一個值是該元素關(guān)聯(lián)的數(shù)值。比如,除了可以ar[43] = “overripe”這樣找到一個數(shù)據(jù),map還可以通過ar[“banana”] = “overripe”這樣的方法找到一個數(shù)據(jù)。如果你想獲得其中的元素信息,通過輸入元素的全名就可以輕松實現(xiàn)。
接下來進(jìn)一步了解他們的一些操作: 文檔中set的插入操作有三種,分別為: 
其中pair是一種模板類型,其中包含兩個數(shù)據(jù)值,兩個數(shù)據(jù)的類型可以不同的。
template <class T1, class T2> struct pair{ typedef T1 first_type; typedef T2 second_type; T1 first; T2 second;}看下面代碼:
#include <iostream>#include <stdlib.h>#include <utility>#include <string>using namespace std;int main() { pair <string, double> 運行結(jié)果為:
三種insert的代碼:
#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>#include <stdlib.h>#include <set>using namespace std;int main(){ set<int> myset; set<int>::iterator it; pair<set<int>::iterator, bool> ret; //直接插入 myset.insert(4); myset.insert(1); myset.insert(7); myset.insert(5); ret = myset.insert(1); // 1已經(jīng)插入過,不能重復(fù)插入 if (ret.second == false) { it = ret.first; // "it" 現(xiàn)在所指是1 } //找位置插入 myset.insert(it, 2); myset.insert(it, 8); int a[] = { 5, 15 }; //插入一段數(shù)據(jù) myset.insert(a, a + 2);//僅插入了15 cout << "myset contains:"<<endl; for (it = myset.begin(); it != myset.end(); it++) { cout << " " << *it; } cout << endl; system("pause"); return 0;運行結(jié)果為: 
同樣刪除操作也對應(yīng)有三種:
測試代碼如下:
運行結(jié)果如下: 
map的應(yīng)用實例,統(tǒng)計水果出現(xiàn)的次數(shù)并排序
#include <algorithm>#include <stdlib.h>#include <map>#include <string>#include <vector>struct Min{ bool Operator()(pair<string, int> p1, pair<string, int> p2) { return p1.second > p2.second; }};void HeapSort(){ vector<string> v1 = { "蘋果", "香蕉", "蘋果" , "蘋果", "蘋果", "香蕉" , "蘋果", "草莓" }; map<string, int> fruit; //用map統(tǒng)計次數(shù) for (size_t i = 0; i < v1.size(); i++) { fruit[v1[i]]++; } // 用heap排序 vector<pair<string, int>> vec; map<string, int>::iterator it = fruit.begin(); //while (it != fruit.end()) //{ // vec.push_back(pair<string, int>(it->first, it->second)); // it++; //} vec.insert(vec.begin(), fruit.begin(), fruit.end()); make_heap(vec.begin(), vec.end(), Min()); sort_heap(vec.begin(), vec.end(), Min()); int K = 3; for (size_t i = 0; (i < K) && (i < vec.size()); i++) { cout << vec[i].first << "--" << vec[i].second << endl; }}int main(){ HeapSort(); system("pause"); return 0;}運行結(jié)果為: 
新聞熱點
疑難解答