小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

map的詳細(xì)用法

 honeyeyo 2016-08-16
  1. map的詳細(xì)用法:  
      map是STL的一個(gè)關(guān)聯(lián)容器,它提供一對(duì)一(其中第一個(gè)可以稱為關(guān)鍵字,每個(gè)關(guān)鍵字只能在map中出現(xiàn)一次,第二個(gè)可能稱為該關(guān)鍵字的值)的數(shù)據(jù)處理能力,由于這個(gè)特性,它完成有可能在我們處理一對(duì)一數(shù)據(jù)的時(shí)候,在編程上提供快速通道。這里說下map內(nèi)部數(shù)據(jù)的組織,map內(nèi)部自建一顆紅黑樹(一種非嚴(yán)格意義上的平衡二叉樹),這顆樹具有對(duì)數(shù)據(jù)自動(dòng)排序的功能,所以在map內(nèi)部所有的數(shù)據(jù)都是有序的,后邊我們會(huì)見識(shí)到有序的好處。
下面舉例說明什么是一對(duì)一的數(shù)據(jù)映射。比如一個(gè)班級(jí)中,每個(gè)學(xué)生的學(xué)號(hào)跟他的姓名就存在著一一映射的關(guān)系,這個(gè)模型用map可能輕易描述,很明顯學(xué)號(hào)用int描述,姓名用字符串描述(本篇文章中不用char *來描述字符串,而是采用STL中string來描述),下面給出map描述代碼:
  1. map<int, string> mapStudent;  
1.map的構(gòu)造函數(shù)
map共提供了6個(gè)構(gòu)造函數(shù),這塊涉及到內(nèi)存分配器這些東西,略過不表,在下面我們將接觸到一些map的構(gòu)造方法,這里要說下的就是,我們通常用如下方法構(gòu)造一個(gè)map:
  1. map<int, string> mapStudent;  
2.數(shù)據(jù)的插入
在構(gòu)造map容器后,我們就可以往里面插入數(shù)據(jù)了。這里講三種插入數(shù)據(jù)的方法:
第一種:用insert函數(shù)插入pair數(shù)據(jù),下面舉例說明(以下代碼雖然是隨手寫的,應(yīng)該可以在VC和GCC下編譯通過,大家可以運(yùn)行下看什么效果,在VC下請(qǐng)加入這條語句,屏蔽4786警告  #pragma warning (disable:4786) )
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;//pair<int,string>p;p=make_pair(v1,v2);<span style="color: rgb(255, 0, 0); background-color: rgb(240, 248, 255); font-family: Arial; font-size: 13px; "> </span>  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        map<int, string>::iterator  iter;  
  12.        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  13.        {  
  14.           cout<<iter->first<<"  "<<iter->second<<endl;  
  15.        }  
  16. }  

  1. make_pair()//返回類型為對(duì)應(yīng)的pair類型  
  2. 無需寫出類別,就可以生成一個(gè)pair對(duì)象  
  3. 例:  
  4. make_pair(1,'@')  
  5. 而不必費(fèi)力的寫成  
  6. pair<int ,char>(1,'@')  
第二種:用insert函數(shù)插入value_type數(shù)據(jù),下面舉例說明
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(map<int, string>::value_type (1, "student_one"));  
  9.        mapStudent.insert(map<int, string>::value_type (2, "student_two"));  
  10.        mapStudent.insert(map<int, string>::value_type (3, "student_three"));  
  11.        map<int, string>::iterator  iter;  
  12.        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  13.        {  
  14.            cout<<iter->first<<" "<<iter->second<<endl;  
  15.        }  
  16. }  
第三種:用數(shù)組方式插入數(shù)據(jù),下面舉例說明
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent[1] =  "student_one";  
  9.        mapStudent[2] =  "student_two";  
  10.        mapStudent[3] =  "student_three";  
  11.        map<int, string>::iterator  iter;  
  12.        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  13.        {  
  14.           cout<<iter->first<<"   "<<iter->second<<endl;  
  15.        }  
  16. }  
以上三種用法,雖然都可以實(shí)現(xiàn)數(shù)據(jù)的插入,但是它們是有區(qū)別的,當(dāng)然了第一種和第二種在效果上是完成一樣的,用insert函數(shù)插入數(shù)據(jù),在數(shù)據(jù)的插入上涉及到集合的唯一性這個(gè)概念,即當(dāng)map中有這個(gè)關(guān)鍵字時(shí),insert操作是插入數(shù)據(jù)不了的,但是用數(shù)組方式就不同了,它可以覆蓋以前該關(guān)鍵字對(duì)應(yīng)的值,用程序說明
  1. mapStudent.insert(map<int, string>::value_type (1, "student_one"));  
  2. mapStudent.insert(map<int, string>::value_type (1, "student_two"));  
上面這兩條語句執(zhí)行后,map中1這個(gè)關(guān)鍵字對(duì)應(yīng)的值是"student_one",第二條語句并沒有生效,那么這就涉及到我們?cè)趺粗纈nsert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程序如下
  1. Pair<map<int, string>::iterator, bool> Insert_Pair;  
  2. Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));  
我們通過pair的第二個(gè)變量來知道是否插入成功,它的第一個(gè)變量返回的是一個(gè)map的迭代器,如果插入成功的話Insert_Pair.second應(yīng)該是true的,否則為false。
下面給出完成代碼,演示插入成功與否問題
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        Pair<map<int, string>::iterator, bool> Insert_Pair;  
  9.        Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));  
  10.        If(Insert_Pair.second == true)  
  11.        {  
  12.              cout<<"Insert Successfully"<<endl;  
  13.        }  
  14.        Else  
  15.        {  
  16.               cout<<"Insert Failure"<<endl;  
  17.        }  
  18.        Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));  
  19.        If(Insert_Pair.second == true)  
  20.        {  
  21.               cout<<"Insert Successfully"<<endl;  
  22.        }  
  23.        Else  
  24.        {  
  25.               cout<<"Insert Failure"<<endl;  
  26.        }  
  27.        map<int, string>::iterator  iter;  
  28.        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  29.        {  
  30.             cout<<iter->first<<"   "<<iter->second<<endl;  
  31.        }  
  32. }  
大家可以用如下程序,看下用數(shù)組插入在數(shù)據(jù)覆蓋上的效果
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent[1] =  "student_one";  
  9.        mapStudent[1] =  "student_two";  
  10.        mapStudent[2] =  "student_three";  
  11.        map<int, string>::iterator  iter;  
  12.        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  13.        {  
  14.           cout<<iter->first<<"   "<<iter->second<<endl;  
  15.        }  
  16. }  
3.map的大小
在往map里面插入了數(shù)據(jù),我們?cè)趺粗喇?dāng)前已經(jīng)插入了多少數(shù)據(jù)呢,可以用size函數(shù),用法如下:
  1. int nSize = mapStudent.size();  
4.數(shù)據(jù)的遍歷
這里也提供三種方法,對(duì)map進(jìn)行遍
第一種:應(yīng)用前向迭代器,上面舉例程序中到處都是了,略過不表
第二種:應(yīng)用反相迭代器,下面舉例說明,要體會(huì)效果,請(qǐng)自個(gè)動(dòng)手運(yùn)行程序
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        map<int, string>::reverse_iterator  iter;  
  12.        for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)  
  13.        {  
  14.           cout<<iter->first<<"   "<<iter->second<<endl;  
  15.        }  
  16. }  
第三種:用數(shù)組方式,程序說明如下
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        int nSize = mapStudent.size()  
  12. //此處有誤,應(yīng)該是 for(int nIndex = 1; nIndex <= nSize; nIndex++)   
  13. //by rainfish  
  14.        for(int nIndex = 0; nIndex < nSize; nIndex++)  
  15.        {  
  16.            cout<<mapStudent[nIndex]<<end;  
  17.        }  
  18. }  
5.數(shù)據(jù)的查找(包括判定這個(gè)關(guān)鍵字是否在map中出現(xiàn))
在這里我們將體會(huì),map在數(shù)據(jù)插入時(shí)保證有序的好處。
要判定一個(gè)數(shù)據(jù)(關(guān)鍵字)是否在map中出現(xiàn)的方法比較多,這里標(biāo)題雖然是數(shù)據(jù)的查找,在這里將穿插著大量的map基本用法。
這里給出三種數(shù)據(jù)查找方法
第一種:用count函數(shù)來判定關(guān)鍵字是否出現(xiàn),其缺點(diǎn)是無法定位數(shù)據(jù)出現(xiàn)位置,由于map的特性,一對(duì)一的映射關(guān)系,就決定了count函數(shù)的返回值只有兩個(gè),要么是0,要么是1,出現(xiàn)的情況,當(dāng)然是返回1了
第二種:用find函數(shù)來定位數(shù)據(jù)出現(xiàn)位置,它返回的一個(gè)迭代器,當(dāng)數(shù)據(jù)出現(xiàn)時(shí),它返回?cái)?shù)據(jù)所在位置的迭代器,如果map中沒有要查找的數(shù)據(jù),它返回的迭代器等于end函數(shù)返回的迭代器,程序說明
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        map<int, string>::iterator iter;  
  12.        iter = mapStudent.find(1);  
  13.        if(iter != mapStudent.end())  
  14.       {  
  15.            cout<<"Find, the value is "<<iter->second<<endl;  
  16.        }  
  17.        Else  
  18.        {  
  19.            cout<<"Do not Find"<<endl;  
  20.        }  
  21. }  
第三種:這個(gè)方法用來判定數(shù)據(jù)是否出現(xiàn),是顯得笨了點(diǎn),但是,我打算在這里講解
Lower_bound函數(shù)用法,這個(gè)函數(shù)用來返回要查找關(guān)鍵字的下界(是一個(gè)迭代器)
Upper_bound函數(shù)用法,這個(gè)函數(shù)用來返回要查找關(guān)鍵字的上界(是一個(gè)迭代器)
例如:map中已經(jīng)插入了1,2,3,4的話,如果lower_bound(2)的話,返回的2,而upper-bound(2)的話,返回的就是3
Equal_range函數(shù)返回一個(gè)pair,pair里面第一個(gè)變量是Lower_bound返回的迭代器,pair里面第二個(gè)迭代器是Upper_bound返回的迭代器,如果這兩個(gè)迭代器相等的話,則說明map中不出現(xiàn)這個(gè)關(guān)鍵字,程序說明
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent[1] =  "student_one";  
  9.        mapStudent[3] =  "student_three";  
  10.        mapStudent[5] =  "student_five";  
  11.        map<int, string>::iterator  iter;  
  12.        iter = mapStudent.lower_bound(2);  
  13.       {  
  14.        //返回的是下界3的迭代器  
  15.        cout<<iter->second<<endl;  
  16.        }  
  17.        iter = mapStudent.lower_bound(3);  
  18.        {  
  19.        //返回的是下界3的迭代器  
  20.        cout<<iter->second<<endl;  
  21.        }  
  22.         iter = mapStudent.upper_bound(2);  
  23.        {  
  24.        //返回的是上界3的迭代器  
  25.         cout<<iter->second<<endl;  
  26.        }  
  27.        iter = mapStudent.upper_bound(3);  
  28.       {  
  29.        //返回的是上界5的迭代器  
  30.        cout<<iter->second<<endl;  
  31. }  
  32.       Pair<map<int, string>::iterator, map<int, string>::iterator> mapPair;  
  33.       mapPair = mapStudent.equal_range(2);  
  34.       if(mapPair.first == mapPair.second)  
  35.       {  
  36.           cout<<"Do not Find"<<endl;  
  37.        }  
  38.        Else  
  39.        {  
  40.             cout<<"Find"<<endl;  
  41.         }  
  42.        mapPair = mapStudent.equal_range(3);  
  43.        if(mapPair.first == mapPair.second)  
  44.        {  
  45.             cout<<"Do not Find"<<endl;  
  46.         }  
  47.        Else  
  48.        {  
  49.            cout<<"Find"<<endl;  
  50.        }  
  51. }  
6.  數(shù)據(jù)的清空與判空
清空map中的數(shù)據(jù)可以用clear()函數(shù),判定map中是否有數(shù)據(jù)可以用empty()函數(shù),它返回true則說明是空map
7.  數(shù)據(jù)的刪除
這里要用到erase函數(shù),它有三個(gè)重載了的函數(shù),下面在例子中詳細(xì)說明它們的用法
  1. #include <map>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.        map<int, string> mapStudent;  
  8.        mapStudent.insert(pair<int, string>(1, "student_one"));  
  9.        mapStudent.insert(pair<int, string>(2, "student_two"));  
  10.        mapStudent.insert(pair<int, string>(3, "student_three"));  
  11.        //如果你要演示輸出效果,請(qǐng)選擇以下的一種,你看到的效果會(huì)比較好  
  12.        //如果要?jiǎng)h除1,用迭代器刪除  
  13.        map<int, string>::iterator iter;  
  14.        iter = mapStudent.find(1);  
  15.        mapStudent.erase(iter);  
  16.        //如果要?jiǎng)h除1,用關(guān)鍵字刪除  
  17.        int n = mapStudent.erase(1);//如果刪除了會(huì)返回1,否則返回0  
  18.        //用迭代器,成片的刪除  
  19.        //一下代碼把整個(gè)map清空  
  20.        mapStudent.earse(mapStudent.begin(), mapStudent.end());  
  21.        //成片刪除要注意的是,也是STL的特性,刪除區(qū)間是一個(gè)前閉后開的集合  
  22.      //自個(gè)加上遍歷代碼,打印輸出吧  
  23. }  
8.其他一些函數(shù)用法
這里有swap,key_comp,value_comp,get_allocator等函數(shù),感覺到這些函數(shù)在編程用的不是很多,略過不表,有興趣的話可以自個(gè)研究
9.排序
這里要講的是一點(diǎn)比較高深的用法了,排序問題,STL中默認(rèn)是采用小于號(hào)來排序的,以上代碼在排序上是不存在任何問題的,因?yàn)樯厦娴年P(guān)鍵字是int型,它本身支持小于號(hào)運(yùn)算,在一些特殊情況,比如關(guān)鍵字是一個(gè)結(jié)構(gòu)體,涉及到排序就會(huì)出現(xiàn)問題,因?yàn)樗鼪]有小于號(hào)操作,insert等函數(shù)在編譯的時(shí)候過不去,下面給出兩個(gè)方法解決這個(gè)問題
第一種:小于號(hào)重載,程序舉例
  1. #include <map>  
  2. #include <string>  
  3. uing namespace std;  
  4. Typedef struct tagStudentInfo  
  5. {  
  6.        int      nID;  
  7.        String   strName;  
  8. }StudentInfo, *PStudentInfo;  //學(xué)生信息  
  9. int main()  
  10. {  
  11.        int nSize;  
  12.        //用學(xué)生信息映射分?jǐn)?shù)  
  13.        map<StudentInfo, int>mapStudent;  
  14.        map<StudentInfo, int>::iterator iter;  
  15.        StudentInfo studentInfo;  
  16.        studentInfo.nID = 1;  
  17.        studentInfo.strName = "student_one"  
  18.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  19.        studentInfo.nID = 2;  
  20.        studentInfo.strName = "student_two";  
  21.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
  22.        for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)  
  23.          cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;  
  24. }  
以上程序是無法編譯通過的,只要重載小于號(hào),就OK了,如下:
  1. Typedef struct tagStudentInfo  
  2. {  
  3.        int      nID;  
  4.        String   strName;  
  5.        Bool operator < (tagStudentInfo const& _A) const  
  6.        {  
  7.               //這個(gè)函數(shù)指定排序策略,按nID排序,如果nID相等的話,按strName排序  
  8.               If(nID < _A.nID)  return true;  
  9.               If(nID == _A.nID) return strName.compare(_A.strName) < 0;  
  10.               Return false;  
  11.        }  
  12. }StudentInfo, *PStudentInfo;  //學(xué)生信息  
第二種:仿函數(shù)的應(yīng)用,這個(gè)時(shí)候結(jié)構(gòu)體中沒有直接的小于號(hào)重載,程序說明
  1. #include <map>  
  2. #include <string>  
  3. using namespace std;  
  4. Typedef struct tagStudentInfo  
  5. {  
  6.        int      nID;  
  7.        String   strName;  
  8. }StudentInfo, *PStudentInfo;  //學(xué)生信息  
  9. class sort  
  10. {  
  11.        Public:  
  12.        Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const  
  13.        {  
  14.               If(_A.nID < _B.nID) return true;  
  15.               If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;  
  16.               Return false;  
  17.        }  
  18. };  
  19. int main()  
  20. {  
  21.        //用學(xué)生信息映射分?jǐn)?shù)  
  22.        map<StudentInfo, int, sort>mapStudent;  
  23.        StudentInfo studentInfo;  
  24.        studentInfo.nID = 1;  
  25.        studentInfo.strName = "student_one";  
  26.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  27.        studentInfo.nID = 2;  
  28.        studentInfo.strName = "student_two";  
  29.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
  30. }  
10.另外
由于STL是一個(gè)統(tǒng)一的整體,map的很多用法都和STL中其它的東西結(jié)合在一起,比如在排序上,這里默認(rèn)用的是小于號(hào),即less<>,如果要從大到小排序呢,這里涉及到的東西很多,在此無法一一加以說明。
還要說明的是,map中由于它內(nèi)部有序,由紅黑樹保證,因此很多函數(shù)執(zhí)行的時(shí)間復(fù)雜度都是log2N的,如果用map函數(shù)可以實(shí)現(xiàn)的功能,而STL  Algorithm也可以完成該功能,建議用map自帶函數(shù),效率高一些。
下面說下,map在空間上的特性,否則,估計(jì)你用起來會(huì)有時(shí)候表現(xiàn)的比較郁悶,由于map的每個(gè)數(shù)據(jù)對(duì)應(yīng)紅黑樹上的一個(gè)節(jié)點(diǎn),這個(gè)節(jié)點(diǎn)在不保存你的數(shù)據(jù)時(shí),是占用16個(gè)字節(jié)的,一個(gè)父節(jié)點(diǎn)指針,左右孩子指針,還有一個(gè)枚舉值(標(biāo)示紅黑的,相當(dāng)于平衡二叉樹中的平衡因子),我想大家應(yīng)該知道,這些地方很費(fèi)內(nèi)存了。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多