1. map的構造函數
map共提供了6個構造函數,這塊涉及到內存分配器這些東西,略過不表,在下面我們將接觸
到一些map的構造方法,這里要說下的就是,我們通常用如下方法構造一個map:
Map<int, string> mapStudent;
2. 數據的插入
在構造map容器后,我們就可以往里面插入數據了。這里講三種插入數據的方法
示例1:
[cpp] view plaincopy
#pragma?warning?(disable:4786)??#include?<string>??#include?<map>??#include?<iostream>??using?namespace?std;????typedef?map<int,string>?mymap;????int?main()??{??????mymap?student;????????????student.insert(pair<int,string>(2,"student-two"));??????student.insert(mymap::value_type(3,"student-three"));???????????student[1]?=?"student-one";?
??? //map會對插入元素按照Key進行排序
????mymap::iterator?iter;??????for(iter?=?student.begin();?iter?!=?student.end();?iter++)??????{??????????cout<<iter->first<<"??"<<iter->second<<endl;??????}??????return?0;??}??
以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種
在效果上是完成一樣的,用insert函數插入數據,在數據的插入上涉及到集合的唯一性這個
概念,即當map中有這個關鍵字時,insert操作是插入數據不了的,但是用數組方式就不同
了,它可以覆蓋以前該關鍵字對應的值,用程序說明
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
上面這兩條語句執行后,map中1這個關鍵字對應的值是“student_one”,第二條語句并沒
有生效,那么這就涉及到我們怎么知道insert語句是否插入成功的問題了,可以用pair來獲
得是否插入成功,程序如下
Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”
));
我們通過pair的第二個變量來知道是否插入成功,它的第一個變量返回的是一個map的迭代
器,如果插入成功的話Insert_Pair.second應該是true的,否則為false。
下面給出完成代碼,演示插入成功與否問題
[cpp] view plaincopy
#pragma?warning?(disable:4786)??#include?<map>??#include?<string>??#include?<iostream>??using?namespace?std;??typedef?map<int,string>?mymap;??int?main()??{??????map<int,?string>?mapStudent;??????pair<?mymap::iterator,?bool>?Insert_Pair;??????Insert_Pair?=?mapStudent.insert(pair<int,string>(1,?"student_one"));??????if(Insert_Pair.second?==?true)??????{??????????cout<<"Insert?Successfully"<<endl;??????}??????else??????{????????????cout<<"Insert?Failure"<<endl;??????}??????Insert_Pair?=?mapStudent.insert(pair<int,?string>(1,"student_two"));??????if(Insert_Pair.second?==?true)??????{??????????cout<<"Insert?Successfully"<<endl;??????}??????else??????{??????????cout<<"Insert?Failure"<<endl;??????}??????mymap::iterator??iter;??????for(iter?=?mapStudent.begin();?iter?!=?mapStudent.end();?iter++)??????{??????????cout<<iter->first<<"??"<<iter->second<<endl;??????}??????return?0;??}??
3. map的大小
在往map里面插入了數據,我們怎么知道當前已經插入了多少數據呢,可以用size函數,用
法如下:
Int nSize = mapStudent.size();
4. 數據的遍歷
這里也提供三種方法,對map進行遍歷
第一種:應用前向迭代器,上面舉例程序中到處都是了,略過不表
第二種:應用反相迭代器,下面舉例說明,要體會效果,請自個動手運行程序
[cpp] view plaincopy
#pragma?warning?(disable:4786)????#include?<map>??#include?<string>??#include?<iostream>??using?namespace?std;??????typedef?map<int,string>?mymap;??int?main()??{??????mymap?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?"));??????mymap::reverse_iterator??iter;??????for(iter?=?mapStudent.rbegin();?iter?!=?mapStudent.rend();?iter++)??????{??????????cout<<iter->first<<?"????"<<iter->second<<endl;??????}??????return?0;??}??
第三種:用數組方式,程序說明如下
[cpp] view plaincopy
#pragma?warning?(disable:4786)????#include?<map>??#include?<string>??#include?<iostream>??using?namespace?std;????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?"));??????int?nSize?=?mapStudent.size();??????for(int?nIndex?=?1;?nIndex?<=?nSize;?nIndex++)????????{??????????cout<<mapStudent[nIndex]<<endl;??????}??????return?0;??}??
5. 數據的查找(包括判定這個關鍵字是否在map中出現)
在這里我們將體會,map在數據插入時保證有序的好處。
要判定一個數據(關鍵字)是否在map中出現的方法比較多,這里標題雖然是數據的查找,
在這里將穿插著大量的map基本用法。
這里給出三種數據查找方法
第一種:用count函數來判定關鍵字是否出現,其缺點是無法定位數據出現位置,由于map的
特性,一對一的映射關系,就決定了count函數的返回值只有兩個,要么是0,要么是1,出
現的情況,當然是返回1了
第二種:用find函數來定位數據出現位置,它返回的一個迭代器,當數據出現時,它返回數
據所在位置的迭代器,如果map中沒有要查找的數據,它返回的迭代器等于end函數返回的迭
代器,程序說明
[cpp] view plaincopy
#pragma?warning?(disable:4786)????#include?<map>??#include?<string>??#include?<iostream>??using?namespace?std;????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;??????}??????return?0;??}??
第三種:這個方法用來判定數據是否出現,是顯得笨了點,但是,我打算在這里講解
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中不
出現這個關鍵字,程序說明
[cpp] view plaincopy
#pragma?warning?(disable:4786)????#include?<map>??#include?<string>??#include?<iostream>??using?namespace?std;????int?main()??{??????map<int,?string>?mapStudent;??????mapStudent[1]?=??"student_one"?;????????????mapStudent[3]?=??"student_three";??????mapStudent[5]?=??"student_five";??????map<int,?string>::iterator??iter;??????iter?=?mapStudent.lower_bound(2);?????????????cout<<iter->second<<endl;????????iter?=?mapStudent.lower_bound(3);????????????cout<<iter->second<<endl;????????iter?=?mapStudent.upper_bound(2);????????????cout<<iter->second<<endl;????????iter?=?mapStudent.upper_bound(3);????????????cout<<iter->second<<endl;????????pair<map<int,?string>::iterator,?map<int,?string>::iterator>?mapPair;????????????????????????mapPair?=?mapStudent.equal_range(2);??????if(mapPair.first?==?mapPair.second)??????{??????????cout<<"Do?not?Find"<<endl;??????}??????else??????{??????????cout<<"Find"<<endl;??????}??????mapPair?=?mapStudent.equal_range(3);??????if(mapPair.first?==?mapPair.second)??????{??????????cout<<"Do?not?Find"<<endl;??????}??????else??????{??????????cout<<"Find"<<endl;??????}??????return?0;??}??
6. 數據的清空與判空
清空map中的數據可以用clear()函數,判定map中是否有數據可以用empty()函數,它返回
true則說明是空map
[cpp] view plaincopy
#pragma?warning?(disable:4786)????#include?<map>??#include?<string>??#include?<iostream>??using?namespace?std;????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);??????mapStudent.erase(iter);????????for(iter?=?mapStudent.begin();?iter?!=?mapStudent.end();?iter++)??????{??????????cout?<<?iter->first<<?"?"<<iter->second<<endl;??????}????????cout<<endl;????????mapStudent.insert(pair<int,?string>(1,?"student_one"));????????????int?n?=?mapStudent.erase(1);??????for(iter?=?mapStudent.begin();?iter?!=?mapStudent.end();?iter++)??????{??????????cout?<<?iter->first<<?"?"<<iter->second<<endl;??????}??????cout<<endl;????????mapStudent.insert(pair<int,?string>(1,?"student_one"));??????????????????mapStudent.erase(mapStudent.begin(),?mapStudent.end());????????????for(iter?=?mapStudent.begin();?iter?!=?mapStudent.end();?iter++)??????{??????????cout?<<?iter->first<<?"?"<<iter->second<<endl;??????}??????cout<<"size:?"<<mapStudent.size()<<endl;????????return?0;??}??
8. 其他一些函數用法
這里有swap,key_comp,value_comp,get_allocator等函數,感覺到這些函數在編程用的不是
很多,略過不表,有興趣的話可以自個研究
9. 排序
這里要講的是一點比較高深的用法了,排序問題,STL中默認是采用小于號來排序的,以上代
碼在排序上是不存在任何問題的,因為上面的關鍵字是int型,它本身支持小于號運算,在
一些特殊情況,比如關鍵字是一個結構體,涉及到排序就會出現問題,因為它沒有小于號操
作,insert等函數在編譯的時候過不去,下面給出兩個方法解決這個問題
第一種:小于號重載,程序舉例
[cpp] view plaincopy
#pragma?warning?(disable:4786)????#include?<map>??#include?<string>??#include?<iostream>??using?namespace?std;????typedef?struct?tagStudentInfo??{??????int??????nID;??????string???strName;??????bool?operator?<?(tagStudentInfo?const&?_A)?const??????{????????????????????if(nID?<?_A.nID)??return?true;??????????if(nID?==?_A.nID)?return?strName.compare(_A.strName)?<?0;??????????return?false;??????}??}StudentInfo,?*PStudentInfo;??????int?main()??{????????????map<StudentInfo,?int>mapStudent;??????StudentInfo?studentInfo;??????studentInfo.nID?=?1;??????studentInfo.strName?=?"student_one";??????mapStudent.insert(pair<StudentInfo,?int>(studentInfo,?90));??????studentInfo.nID?=?2;??????studentInfo.strName?=?"student_two";??????mapStudent.insert(pair<StudentInfo,?int>(studentInfo,?80));????????map<StudentInfo,int>::iterator?iter;??????for(iter?=?mapStudent.begin();?iter?!=?mapStudent.end();?iter?++)??????{??????????cout<<iter->first.nID<<"??"<<iter->first.strName<<"??"<<iter->second<<endl;??????}??????return?0;??}??
10. 另外
由于STL是一個統一的整體,map的很多用法都和STL中其它的東西結合在一起,比如在排序
上,這里默認用的是小于號,即less<>,如果要從大到小排序呢,這里涉及到的東西很多,
在此無法一一加以說明。
還要說明的是,map中由于它內部有序,由紅黑樹保證,因此很多函數執行的時間復雜度都
是log2N的,如果用map函數可以實現的功能,而STL Algorithm也可以完成該功能,建議用
map自帶函數,效率高一些。
下面說下,map在空間上的特性,否則,估計你用起來會有時候表現的比較郁悶,由于map的
每個數據對應紅黑樹上的一個節點,這個節點在不保存你的數據時,是占用16個字節的,一
個父節點指針,左右孩子指針,還有一個枚舉值(標示紅黑的,相當于平衡二叉樹中的平衡
因子),我想大家應該知道,這些地方很費內存了吧,不說了……
總結
以上是生活随笔為你收集整理的map使用简明教程的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。