STL 中map 和 set 的知識點總結
根據不同的場合選擇不同的容器,會有意想不到的收穫。
Map是單詞mapping(對映)的縮寫
Set是單詞set(集合)的意思;
Map和set內部的元素不可以重複,這一點不同於multimap和multiset。
map 和 set使用相同的資料結構,不同的是 其中的Iterator的格式不一樣,map使用pair這種配對的資料,並根據pair中第一個元素的值進行排序,而set的iterator並不具備天生的pair型別的元素,直接根據其中的元素進行排序,下面的偽碼也許可以更清楚的表述:
typedef template<class Key, class Value> rb_tree< pair<Key,Value> > map;
typedef template<class T> rb_tree< T > set;
map的節點是一對資料,set的節點是一個數據。
map的形式 map<type1, type2> mymap;
set的形式 set<type> myset;
一般map是對有關聯的事物儲存,操作。
set是對一個欄位進行儲存,操作。
map:使用關鍵字key來標示每一個成員,相當於字典,把一個值對映成另一個值,如果想建立字典的話,map就是一個很好的選擇。map底層採用的是樹型結構,多數使用平衡二叉樹實現,查詢某一值是常數時間,遍歷起來效果也不錯, 只是每次插入值的時候,會重新構成底層的平衡二叉樹,效率有一定影響
1. 宣告方式:
map<int, string> mapStudent;
2. 資料的插入
在構造map容器後,我們就可以往裡面插入資料了。這裡講三種插入資料的方法:
第一種:用insert函式插入pair資料
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
第二種:用insert函式插入value_type資料
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
第三種:用陣列方式插入資料
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[2] = "student_two";
3. map的大小
在往map裡面插入了資料,我們怎麼知道當前已經插入了多少資料呢,可以用size函式:
Int nSize = mapStudent.size();
4. 資料的遍歷
第一種:應用前向迭代器
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
Cout<<iter->first<<" "<<iter->second<<end;
第二種:應用反相迭代器
map<int, string>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
Cout<<iter->first<<" "<<iter->second<<end;
第三種:用陣列方式
int nSize = mapStudent.size()
for(int nIndex = 1; nIndex <= nSize; nIndex++)
Cout<<mapStudent[nIndex]<<end;
5. 資料的查詢(包括判定這個關鍵字是否在map中出現)
這裡給出三種資料查詢方法
第一種:用count函式來判定關鍵字是否出現,但是無法定位資料出現位置
第二種:用find函式來定位資料出現位置它返回的一個迭代器,
當資料出現時,它返回資料所在位置的迭代器,如果map中沒有要查詢的資料,它返回的迭代器等於end函式返回的迭代器
Int main()
{
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one"));
mapStudent.insert(pair<int, string>(2, “student_two"));
mapStudent.insert(pair<int, string>(3, “student_three"));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
{
Cout<<"Find, the value is "<<iter->second<<endl;
}
Else
{
Cout<<"Do not Find"<<endl;
}
}
第三種:這個方法用來判定資料是否出現
Lower_bound函式用法,這個函式用來返回要查詢關鍵字的下界(是一個迭代器)
Upper_bound函式用法,這個函式用來返回要查詢關鍵字的上界(是一個迭代器)
例如:map中已經插入了1,2,3,4的話,如果lower_bound(2)的話,返回的2,而upper-bound(2)的話,返回的就是3
Equal_range函式返回一個pair,pair裡面第一個變數是Lower_bound返回的迭代器,pair裡面第二個迭代器是Upper_bound返回的迭代器,如果這兩個迭代器相等的話,則說明map中不出現這個關鍵字,程式說明
mapPair = mapStudent.equal_range(2);
if(mapPair.first == mapPair.second)
cout<<"Do not Find"<<endl;
6. 資料的清空與判空
清空map中的資料可以用clear()函式,判定map中是否有資料可以用empty()函式,它返回true則說明是空map
7. 資料的刪除
這裡要用到erase函式,它有三個過載了的函式
迭代器刪除
iter = mapStudent.find(1);
mapStudent.erase(iter);
用關鍵字刪除
Int n = mapStudent.erase(1);//如果刪除了會返回1,否則返回0
用迭代器,成片的刪除
一下程式碼把整個map清空
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//成片刪除要注意的是,也是STL的特性,刪除區間是一個前閉後開的集合
8. 其他一些函式用法
這裡有swap,key_comp,value_comp,get_allocator等函式,有興趣的話可以自個研究
set的基本使用:
定義:
定義一個元素為整數的集合a,可以用
set<int> a;
基本操作:
對集合a中元素的有
插入元素:a.insert(1);
刪除元素(如果存在):a.erase(1);
判斷元素是否屬於集合:if (a.find(1) != a.end()) ...
返回集合元素的個數:a.size()
將集合清為空集:a.clear()
集合的並,交和差
set_union(a.begin(),a.end(),b.begin(),b.end(),insert_iterator<set<int> >(c,c.begin()));
set_intersection(a.begin(),a.end(),b.begin(),b.end(),insert_iterator<set<int> >(c,c.begin()));
set_difference(a.begin(),a.end(),b.begin(),b.end(),insert_iterator<set<int> >(c,c.begin()));
(注意在此前要將c清為空集)。
注意:
很重要的一點,為了實現集合的快速運算,set的實現採用了平衡二叉樹,因此,set中的元素必須是可排序的。如果是自定義的型別,那在定義型別的同時必須給出運算子<的定義
收集資料時,在網路上轉載了一個網友的一篇關於MAP和SET幾個常見問題的博文,寫的很好,相關連結如下: