STL中map用法詳解

由於STL是一個統一的整體,map的很多用法都和STL中其它的東西結合在一起;map中由於它內部有序,由紅黑樹保證,因此很多函數執行的時間複雜度都是log2N的,如果用map函數可以實現的功能,而STL Algorithm也可以完成該功能,建議用map自帶函數,效率高一些……

Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱爲關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱爲該關鍵字的值)的數據處理能力,由於這個特性,它完成有可能在我們處理一對一數據的時候,在編程上提供快速通道。這裏說下map內部數據的組織,map內部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),這顆樹具有對數據自動排序的功能,所以在map內部所有的數據都是有序的,後邊我們會見識到有序的好處。

下面舉例說明什麼是一對一的數據映射。比如一個班級中,每個學生的學號跟他的姓名就存在着一一映射的關係,這個模型用map可能輕易描述,很明顯學號用int描述,姓名用字符串描述(本篇文章中不用char *來描述字符串,而是採用STL中string來描述),下面給出map描述代碼:

Map<int, string> mapStudent;

1.       map的構造函數

map共提供了6個構造函數,這塊涉及到內存分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這裏要說下的就是,我們通常用如下方法構造一個map:

Map<int, string> mapStudent;

2.       數據的插入

在構造map容器後,我們就可以往裏面插入數據了。這裏講三種插入數據的方法:

第一種:用insert函數插入pair數據,下面舉例說明(以下代碼雖然是隨手寫的,應該可以在VC和GCC下編譯通過,大家可以運行下看什麼效果,在VC下請加入這條語句,屏蔽4786警告  #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;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

       Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

第二種:用insert函數插入value_type數據,下面舉例說明

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent.insert(map<int, string>::value_type (1, “student_one”));

       mapStudent.insert(map<int, string>::value_type (2, “student_two”));

       mapStudent.insert(map<int, string>::value_type (3, “student_three”));

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

       Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

第三種:用數組方式插入數據,下面舉例說明

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent[1] =  “student_one”;

       mapStudent[2] =  “student_two”;

       mapStudent[3] =  “student_three”;

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

       Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用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。

下面給出完成代碼,演示插入成功與否問題

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

Pair<map<int, string>::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;

       }

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

       Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

大家可以用如下程序,看下用數組插入在數據覆蓋上的效果

#include <map>

#include <string>

#include <iostream>

Using namespace std;

Int main()

{

       Map<int, string> mapStudent;

       mapStudent[1] =  “student_one”;

       mapStudent[1] =  “student_two”;

       mapStudent[2] =  “student_three”;

       map<int, string>::iterator  iter;

       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

       Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

3.       map的大小

在往map裏面插入了數據,我們怎麼知道當前已經插入了多少數據呢,可以用size函數,用法如下:

Int nSize = mapStudent.size();

4.       數據的遍歷

這裏也提供三種方法,對map進行遍歷

第一種:應用前向迭代器,上面舉例程序中到處都是了,略過不表

第二種:應用反相迭代器,下面舉例說明,要體會效果,請自個動手運行程序

#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>::reverse_iterator  iter;

       for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)

{

       Cout<<iter->first<<”   ”<<iter->second<<end;

}

}

第三種:用數組方式,程序說明如下

#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++)


//by rainfish

       for(int nIndex = 0; nIndex < nSize; nIndex++)

{

       Cout<<mapStudent[nIndex]<<end;

}

}

5.       數據的查找(包括判定這個關鍵字是否在map中出現)

在這裏我們將體會,map在數據插入時保證有序的好處。

要判定一個數據(關鍵字)是否在map中出現的方法比較多,這裏標題雖然是數據的查找,在這裏將穿插着大量的map基本用法。

這裏給出三種數據查找方法

第一種:用count函數來判定關鍵字是否出現,其缺點是無法定位數據出現位置,由於map的特性,一對一的映射關係,就決定了count函數的返回值只有兩個,要麼是0,要麼是1,出現的情況,當然是返回1了

第二種:用find函數來定位數據出現位置,它返回的一個迭代器,當數據出現時,它返回數據所在位置的迭代器,如果map中沒有要查找的數據,它返回的迭代器等於end函數返回的迭代器,程序說明

#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;

}

}

第三種:這個方法用來判定數據是否出現,是顯得笨了點,但是,我打算在這裏講解

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中不出現這個關鍵字,程序說明

#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);

{

       //返回的是下界3的迭代器

       Cout<<iter->second<<endl;

}

iter = mapStudent.lower_bound(3);

{

       //返回的是下界3的迭代器

       Cout<<iter->second<<endl;

}

 

iter = mapStudent.upper_bound(2);

{

       //返回的是上界3的迭代器

       Cout<<iter->second<<endl;

}

iter = mapStudent.upper_bound(3);

{

       //返回的是上界5的迭代器

       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;
}

}

6.       數據的清空與判空

清空map中的數據可以用clear()函數,判定map中是否有數據可以用empty()函數,它返回true則說明是空map

7.       數據的刪除

這裏要用到erase函數,它有三個重載了的函數,下面在例子中詳細說明它們的用法

#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”));

 

//如果你要演示輸出效果,請選擇以下的一種,你看到的效果會比較好

       //如果要刪除1,用迭代器刪除

       map<int, string>::iterator iter;

       iter = mapStudent.find(1);

       mapStudent.erase(iter);

 

       //如果要刪除1,用關鍵字刪除

       Int n = mapStudent.erase(1);//如果刪除了會返回1,否則返回0

 

       //用迭代器,成片的刪除

       //一下代碼把整個map清空

       mapStudent.earse(mapStudent.begin(), mapStudent.end());

       //成片刪除要注意的是,也是STL的特性,刪除區間是一個前閉後開的集合

 

       //自個加上遍歷代碼,打印輸出吧

}

8.       其他一些函數用法

這裏有swap,key_comp,value_comp,get_allocator等函數,感覺到這些函數在編程用的不是很多,略過不表,有興趣的話可以自個研究

9.       排序

這裏要講的是一點比較高深的用法了,排序問題,STL中默認是採用小於號來排序的,以上代碼在排序上是不存在任何問題的,因爲上面的關鍵字是int 型,它本身支持小於號運算,在一些特殊情況,比如關鍵字是一個結構體,涉及到排序就會出現問題,因爲它沒有小於號操作,insert等函數在編譯的時候過不去,下面給出兩個方法解決這個問題

第一種:小於號重載,程序舉例

#include <map>

#include <string>

Using namespace std;

Typedef struct tagStudentInfo

{

       Int      nID;

       String   strName;

}StudentInfo, *PStudentInfo;  //學生信息

 

Int main()

{

    int nSize;

       //用學生信息映射分數

       map<StudentInfo, int>mapStudent;

    map<StudentInfo, int>::iterator iter;

       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));

 

for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)

    cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;

 

}

以上程序是無法編譯通過的,只要重載小於號,就OK了,如下:

Typedef struct tagStudentInfo

{

       Int      nID;

       String   strName;

       Bool operator < (tagStudentInfo const& _A) const

       {

              //這個函數指定排序策略,按nID排序,如果nID相等的話,按strName排序

              If(nID < _A.nID)  return true;

              If(nID == _A.nID) return strName.compare(_A.strName) < 0;

              Return false;

       }

}StudentInfo, *PStudentInfo;  //學生信息

第二種:仿函數的應用,這個時候結構體中沒有直接的小於號重載,程序說明

#include <map>

#include <string>

Using namespace std;

Typedef struct tagStudentInfo

{

       Int      nID;

       String   strName;

}StudentInfo, *PStudentInfo;  //學生信息

 

Classs sort

{

       Public:

       Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const

       {

              If(_A.nID < _B.nID) return true;

              If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;

              Return false;

       }

};

 

Int main()

{

       //用學生信息映射分數

       Map<StudentInfo, int, sort>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));

}

10.   另外

由於STL是一個統一的整體,map的很多用法都和STL中其它的東西結合在一起,比如在排序上,這裏默認用的是小於號,即less<>,如果要從大到小排序呢,這裏涉及到的東西很多,在此無法一一加以說明。

還要說明的是,map中由於它內部有序,由紅黑樹保證,因此很多函數執行的時間複雜度都是log2N的,如果用map函數可以實現的功能,而STL  Algorithm也可以完成該功能,建議用map自帶函數,效率高一些。

下面說下,map在空間上的特性,否則,估計你用起來會有時候表現的比較鬱悶,由於map的每個數據對應紅黑樹上的一個節點,這個節點在不保存你的數據時,是佔用16個字節的,一個父節點指針,左右孩子指針,還有一個枚舉值(標示紅黑的,相當於平衡二叉樹中的平衡因子),我想大家應該知道,這些地方很費內存了吧,不說了……


--------------------------------------------------

om摘  要 本文深入剖析了C++標準模板庫(STL)中的map,對其概念和用法進行了深入探討,並結合實例,詳細闡述了map的相關用法。

    關鍵詞 STL;map;插入;刪除;排序  
1 map概述     STL(Standard Template Library 標準模版庫)是C++標準程序庫的核心,它深刻 影響了標準程序庫的整體結構。STL是一個範型(generic)程序庫,提供一系列軟件方案,利用先進、高效的算法來管理數據。STL的好處在於封裝了許多數據結構和算法(algorithm),map就是其典型代表。 map是STL的一個關聯容器,它提供一對一(key/value 其中第一個可以稱爲關鍵字,每個關鍵字只能在map中出現一次,第二個可以稱爲該關鍵字的值)的數據處理能力,由於這個特性,在處理一對一數據的時候,可以提供編程的快速通道。 2 map的用法     假設一個班級中,每個學生的學號和他的姓名存在一一映射的關係,這個模型用map可以輕易描述,學號用int描述,姓名用字符串描述,給出map的描述代碼:map<int, string> mapStudent 。 2.1  插入數據     map元素的插入功能可以通過以下操作實現:     第一種通過主鍵獲取map中的元素,如果獲取到,則返回對應結點對應的實值(存儲在map結點中的對象)。但這個 方法會產生副作用,如果以主鍵“key”獲取結點的實值,在map中並不存在這個結點,則會直接向map中插入以key爲主鍵的結點,並返回這個結點,這時可以對其進行賦值操作。但如果在map中存在了以key爲主鍵的結點,則會返回這個結點的實值,如果此時進行復制操作,則會出現原來結點被新結點覆蓋的危險,如果是指針類型則會出現內存泄漏等 問題 。由於存在這樣的副作用,不建議使用這種方法進行元素的插入。     第二種插入value_type數據。     insert方法接口原型:pair<ierator, bool> insert(const value_type& X) 該方法需要構建一個鍵值對,即value_type,然後調用insert方法,在該方法中實現根據鍵值對中的key值查找對應的結點,如果查找到,則不插入當前結點,並返回找到的那個結點,並將pair中的第二個量置爲false;否則插入當前結點,並返回插入的當前結點,且第二個值置爲 true。在插入結點的時候,在map內部會重新構造一個新的value_type結點並將傳入的X進行copy構造,內部使用了placement new方式,通過內存分配器分配一個map結點,再在獲取的結點空間中調用value_type構造函數。所以調用者構造的鍵值對value_type是一個臨時變量,不會加入到map中(不要被引用操作符迷惑,這裏僅僅是傳參效率上的考慮)。這種結點插入的方式是安全的,建議使用這種方式向map中插入元素,並判斷返回的插入結果,根據插入結果進行後續處理。 #pragma warning(disable:4786) #include <map> #include <string> #include <iostream> using namespace std; int main() { map<int,string> mapStudent; mapStudent.insert(map<int,string>::value_type(1,"one")); mapStudent.insert(map<int,string>::value_type(2,"two")); mapStudent.insert(map<int,string>::value_type(3,"three")); map<int,string>::iterator iter; for(iter=mapStudent.begin();iter!=mapStudent.end();iter++) {      cout<<iter->first<<" "<<iter->second<<endl; } } 2.2  map的大小     往map中插入了數據,可以用size()函數得到當前已經插入了多少數據: int nSize=mapStudent.size() 2.3  排序     STL中默認是採用小於號來排序的,以上代碼在排序上是不存在任何問題的,因爲上面例子中的關鍵字是int型,它本身支持小於號運算。在一些特殊情況下,比如關鍵字是一個結構體,涉及到排序就會出現問題,因爲它沒有小於號運算,insert等函數在編譯的時候過不去。給出一種方法解決排序問題小於號重載。 #pragma warning(disable:4786) #include <map> #include <string> #include <iostream> using namespace std; typedef struct tagStudentInfo { int nID; string strName; } StudentInfo, *PStudentInfo;   // 學生信息 int main() { //用學生信息映射分數 map<StudentInfo, int> mapStudent; StudentInfo studentInfo; studentInfo.nID=2; studentInfo.strName="one"; mapStudent.insert(map<StudentInfo, int>::value_type (studentInfo,90)); studentInfo.nID=1; studentInfo.strName="two"; mapStudent.insert(map<StudentInfo, int>::value_type (studentInfo,80)); } 以上程序無法編譯通過,需要重載小於號。 typedef struct tagStudentInfo { int nID; string strName; bool operator <(tagStudentInfo const& _A) const {//這個函數指定排序策略,按nID排序,如果nID相等按strName排序 if(nID<_A.nID)  return true; if(nID==_A.nID) return strName.compare(_A.strName) <0; return false; } } StudentInfo, *PStudentInfo; 
2.4  map中結點的刪除操作     兩種 應用 場景:     第一種:一次只從map中查找一個結點並刪除。     這種刪除較爲簡單,只需要根據鍵值在map中查找,並將找到的結點刪除就可以了。中國論文聯盟http://www.lwlm.com #pragma warning(disable:4786) #include <map> #include <string> #include <iostream> using namespace std; void main() { map<int,string> mapStudent; mapStudent.insert(map<int,string>::value_type(1,"one")); mapStudent.insert(map<int,string>::value_type(2,"two")); mapStudent.insert(map<int,string>::value_type(3,"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; } }     第二種:從map中遍歷檢查所有結點,將符合條件的結點刪除。     應用場景描述:系統定期檢查垃圾會話(會話放在map表中),根據當前系統時間減去會話最近活動時間,得到會話最近未活動時間間隔,如果這個間隔超過預定的值(認爲會話是垃圾會話,可以被強制刪除),則刪除該會話並從map中刪除這個結點。     做法有兩種:     (1)先遍歷一遍map,找出所有滿足條件的結點,將每一個對應結點的key放入一個vector中,後面再從vector中依次取出key值,做單結點刪除操作。     這種 方法是很原始且效率低下的做法,之所以會這樣實現,是由於開發人員對map使用不甚瞭解的基礎上做出來的。這種方法不但增加了中間處理過程的系統開銷(多構建了一個緩存空間),而且多了N(待刪除結點的結點數)次的查詢操作,對於經常出現的操作,這種低效是不可容忍的。     (2)在map遍歷的過程中,完成對符合條件結點的刪除操作(這個是由map本身數據結構特性保證的)。在遍歷的過程中最主要的就是怎麼保證刪除的結點在刪除前將指針指向下一個結點(這一點正是我們要做的),在刪除了當前結點後,map中的數據結構能夠保證後續的迭代器指針是有效的,而且後續的結點都沒有遍歷過(這個特性是由map底層的紅黑樹的相關操作保證的)。所以需要將迭代器指向下一個結點後再刪除當前符合條件的結點。 #pragma warning(disable:4786) #include <map> #include <string> #include <iostream> using namespace std; void main() { map<int,string> mapStudent; mapStudent.insert(map<int,string>::value_type(1,"one")); mapStudent.insert(map<int,string>::value_type(2,"two")); mapStudent.insert(map<int,string>::value_type(3,"three")); map<int,string>::iterator iter; string aa="three"; iter=mapStudent.begin(); for(;iter!=mapStudent.end();) {      if((iter->second)>=aa)      {          //滿足刪除條件,刪除當前結點,並指向下面一個結點               mapStudent.erase(iter++);      }      else      {      //條件不滿足,指向下面一個結點      iter++;      } } for(iter=mapStudent.begin();iter!=mapStudent.end();iter++) {      cout<<iter->first<<" "<<iter->second<<endl; } }     這種刪除方式也是STL源碼一書中推薦的方式。比較一下mapStudent.erase(iter++)和mapStudent.erase(iter); iter++;這個執行序列。不妨做個簡單的測試,看看彙編執行下的執行序列: void func(int a) {} int main(int, char**) { int iPos=0; func(iPos++); }     函數調用func(iPos++)執行序列:將iPos放入寄存器edx中(緩存起來),隨機對 iPos做加操作 inc dword ptr [ebp-0x04]。也就是說函數調用中的iPos++的執行時期在函數體func執行前就已經完成,而函數體中的參數使用的是iPos未做加操作之前的副本。再 分析 mapStudent.erase(iter++)語句,map中在刪除iter的時候,先將iter做緩存,然後執行iter++使之指向下一個結點,再進入erase函數體中執行刪除操作,刪除時使用的iter就是緩存下來的iter(也就是當前iter(做了加操作之後的iter)所指向結點的上一個結點)。     根據以上分析,可以看出mapStudent.erase(iter++)和map Student.erase(iter); iter++;這個執行序列是不相同的。前者在erase執行前進行了加操作,在iter被刪除(失效)前進行了加操作,是安全的;後者是在erase執行後才進行加操作,而此時iter已經被刪除(當前的迭代器已經失效了),對一個已經失效的迭代器進行加操作,行爲是不可預期的,這種寫法勢必會導致 map操作的失敗並引起進程的異常。 3 結束語     充分利用map的強大功能,可以使程序員的工作量大大減輕,採用傳統方法編寫的許多行代碼,往往通過調用一兩個算法模板就可實現。map技術可以讓程序員編寫出簡潔而高效的代碼,使編程工作更加簡單而有效。 參考 文獻 [1] Nicolai M.Josuttis. C++標準程序庫[M]. 武漢: 華中 科技 大學出版社,2006. [2] Scott Meyers. Effective STL中文版50條有效使用STL的經驗[M]. 北京: 清華大學出版社,2006. [3] 侯捷. STL源碼剖析[M]. 武漢: 華中科技大學出版社,2002. [4] 王昌晶,薛錦雲. 從C++到STL[J]. 江西師範大學學報,2004(8):231-234.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章