Map是STL的一個(gè)關(guān)聯(lián)容器,它提供一對一(其中第一個(gè)可以稱(chēng)為關(guān)鍵字,每個(gè)關(guān)鍵字只能在map中出現一次,第二個(gè)可能稱(chēng)為該關(guān)鍵字的值)的數據處理能力,由于這個(gè)特性,它完成有可能在我們處理一對一數據的時(shí)候,在編程上提供快速通道。這里說(shuō)下map內部數據的組織,map內部自建一顆紅黑樹(shù)(一種非嚴格意義上的平衡二叉樹(shù)),這顆樹(shù)具有對數據自動(dòng)排序的功能,所以在map內部所有的數據都是有序的,后邊我們會(huì )見(jiàn)識到有序的好處。
下面舉例說(shuō)明什么是一對一的數據映射。比如一個(gè)班級中,每個(gè)學(xué)生的學(xué)號跟他的姓名就存在著(zhù)一一映射的關(guān)系,這個(gè)模型用map可能輕易描述,很明顯學(xué)號用int描述,姓名用字符串描述(本篇文章中不用char *來(lái)描述字符串,而是采用STL中string來(lái)描述),下面給出map描述代碼:
Map<int, string> mapStudent;
1. map的構造函數
map共提供了6個(gè)構造函數,這塊涉及到內存分配器這些東西,略過(guò)不表,在下面我們將接觸到一些map的構造方法,這里要說(shuō)下的就是,我們通常用如下方法構造一個(gè)map:
Map<int, string> mapStudent;
2. 數據的插入
在構造map容器后,我們就可以往里面插入數據了。這里講三種插入數據的方法:
第一種:用insert函數插入pair數據,下面舉例說(shuō)明(以下代碼雖然是隨手寫(xiě)的,應該可以在VC和GCC下編譯通過(guò),大家可以運行下看什么效果,在VC下請加入這條語(yǔ)句,屏蔽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數據,下面舉例說(shuō)明
#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;
}
}
第三種:用數組方式插入數據,下面舉例說(shuō)明
#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;
}
}
以上三種用法,雖然都可以實(shí)現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函數插入數據,在數據的插入上涉及到集合的唯一性這個(gè)概念,即當map中有這個(gè)關(guān)鍵字時(shí),insert操作是插入數據不了的,但是用數組方式就不同了,它可以覆蓋以前該關(guān)鍵字對應的值,用程序說(shuō)明
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
上面這兩條語(yǔ)句執行后,map中1這個(gè)關(guān)鍵字對應的值是“student_one”,第二條語(yǔ)句并沒(méi)有生效,那么這就涉及到我們怎么知道insert語(yǔ)句是否插入成功的問(wèn)題了,可以用pair來(lái)獲得是否插入成功,程序如下
Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
我們通過(guò)pair的第二個(gè)變量來(lái)知道是否插入成功,它的第一個(gè)變量返回的是一個(gè)map的迭代器,如果插入成功的話(huà)Insert_Pair.second應該是true的,否則為false。
下面給出完成代碼,演示插入成功與否問(wèn)題
#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里面插入了數據,我們怎么知道當前已經(jīng)插入了多少數據呢,可以用size函數,用法如下:
Int nSize = mapStudent.size();
4. 數據的遍歷
這里也提供三種方法,對map進(jìn)行遍歷
第一種:應用前向迭代器,上面舉例程序中到處都是了,略過(guò)不表
第二種:應用反相迭代器,下面舉例說(shuō)明,要體會(huì )效果,請自個(gè)動(dòng)手運行程序
#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;
}
}
第三種:用數組方式,程序說(shuō)明如下
#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. 數據的查找(包括判定這個(gè)關(guān)鍵字是否在map中出現)
在這里我們將體會(huì ),map在數據插入時(shí)保證有序的好處。
要判定一個(gè)數據(關(guān)鍵字)是否在map中出現的方法比較多,這里標題雖然是數據的查找,在這里將穿插著(zhù)大量的map基本用法。
這里給出三種數據查找方法
第一種:用count函數來(lái)判定關(guān)鍵字是否出現,其缺點(diǎn)是無(wú)法定位數據出現位置,由于map的特性,一對一的映射關(guān)系,就決定了count函數的返回值只有兩個(gè),要么是0,要么是1,出現的情況,當然是返回1了
第二種:用find函數來(lái)定位數據出現位置,它返回的一個(gè)迭代器,當數據出現時(shí),它返回數據所在位置的迭代器,如果map中沒(méi)有要查找的數據,它返回的迭代器等于end函數返回的迭代器,程序說(shuō)明
#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;
}
}
第三種:這個(gè)方法用來(lái)判定數據是否出現,是顯得笨了點(diǎn),但是,我打算在這里講解
Lower_bound函數用法,這個(gè)函數用來(lái)返回要查找關(guān)鍵字的下界(是一個(gè)迭代器)
Upper_bound函數用法,這個(gè)函數用來(lái)返回要查找關(guān)鍵字的上界(是一個(gè)迭代器)
例如:map中已經(jīng)插入了1,2,3,4的話(huà),如果lower_bound(2)的話(huà),返回的2,而upper-bound(2)的話(huà),返回的就是3
Equal_range函數返回一個(gè)pair,pair里面第一個(gè)變量是Lower_bound返回的迭代器,pair里面第二個(gè)迭代器是Upper_bound返回的迭代器,如果這兩個(gè)迭代器相等的話(huà),則說(shuō)明map中不出現這個(gè)關(guān)鍵字,程序說(shuō)明
#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則說(shuō)明是空map
7. 數據的刪除
這里要用到erase函數,它有三個(gè)重載了的函數,下面在例子中詳細說(shuō)明它們的用法
#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”));
//如果你要演示輸出效果,請選擇以下的一種,你看到的效果會(huì )比較好
//如果要刪除1,用迭代器刪除
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);
//如果要刪除1,用關(guān)鍵字刪除
Int n = mapStudent.erase(1);//如果刪除了會(huì )返回1,否則返回0
//用迭代器,成片的刪除
//一下代碼把整個(gè)map清空
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//成片刪除要注意的是,也是STL的特性,刪除區間是一個(gè)前閉后開(kāi)的集合
//自個(gè)加上遍歷代碼,打印輸出吧
}
8. 其他一些函數用法
這里有swap,key_comp,value_comp,get_allocator等函數,感覺(jué)到這些函數在編程用的不是很多,略過(guò)不表,有興趣的話(huà)可以自個(gè)研究
9. 排序
這里要講的是一點(diǎn)比較高深的用法了,排序問(wèn)題,STL中默認是采用小于號來(lái)排序的,以上代碼在排序上是不存在任何問(wèn)題的,因為上面的關(guān)鍵字是int型,它本身支持小于號運算,在一些特殊情況,比如關(guān)鍵字是一個(gè)結構體,涉及到排序就會(huì )出現問(wèn)題,因為它沒(méi)有小于號操作,insert等函數在編譯的時(shí)候過(guò)不去,下面給出兩個(gè)方法解決這個(gè)問(wèn)題
第一種:小于號重載,程序舉例
#include <map>
#include <string>
Using namespace std;
Typedef struct tagStudentInfo
{
Int nID;
String strName;
}StudentInfo, *PStudentInfo; //學(xué)生信息
Int main()
{
int nSize;
//用學(xué)生信息映射分數
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;
}
以上程序是無(wú)法編譯通過(guò)的,只要重載小于號,就OK了,如下:
Typedef struct tagStudentInfo
{
Int nID;
String strName;
Bool operator < (tagStudentInfo const& _A) const
{
//這個(gè)函數指定排序策略,按nID排序,如果nID相等的話(huà),按strName排序
If(nID < _A.nID) return true;
If(nID == _A.nID) return strName.compare(_A.strName) < 0;
Return false;
}
}StudentInfo, *PStudentInfo; //學(xué)生信息
第二種:仿函數的應用,這個(gè)時(shí)候結構體中沒(méi)有直接的小于號重載,程序說(shuō)明
#include <map>
#include <string>
Using namespace std;
Typedef struct tagStudentInfo
{
Int nID;
String strName;
}StudentInfo, *PStudentInfo; //學(xué)生信息
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()
{
//用學(xué)生信息映射分數
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是一個(gè)統一的整體,map的很多用法都和STL中其它的東西結合在一起,比如在排序上,這里默認用的是小于號,即less<>,如果要從大到小排序呢,這里涉及到的東西很多,在此無(wú)法一一加以說(shuō)明。
還要說(shuō)明的是,map中由于它內部有序,由紅黑樹(shù)保證,因此很多函數執行的時(shí)間復雜度都是log2N的,如果用map函數可以實(shí)現的功能,而STL Algorithm也可以完成該功能,建議用map自帶函數,效率高一些。
下面說(shuō)下,map在空間上的特性,否則,估計你用起來(lái)會(huì )有時(shí)候表現的比較郁悶,由于map的每個(gè)數據對應紅黑樹(shù)上的一個(gè)節點(diǎn),這個(gè)節點(diǎn)在不保存你的數據時(shí),是占用16個(gè)字節的,一個(gè)父節點(diǎn)指針,左右孩子指針,還有一個(gè)枚舉值(標示紅黑的,相當于平衡二叉樹(shù)中的平衡因子),我想大家應該知道,這些地方很費內存了吧,不說(shuō)了……
聯(lián)系客服