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ù)來(lái)判定關(guān)鍵字是否出現(xiàn),其缺點(diǎn)是無(wú)法定位數(shù)據(jù)出現(xiàn)位置,由于map的特性,一對(duì)一的映射關(guān)系,就決定了count函數(shù)的返回值只有兩個(gè),要么是0,要么是1,出現(xiàn)的情況,當(dāng)然是返回1了
第二種:用find函數(shù)來(lái)定位數(shù)據(jù)出現(xiàn)位置,它返回的一個(gè)迭代器,當(dāng)數(shù)據(jù)出現(xiàn)時(shí),它返回?cái)?shù)據(jù)所在位置的迭代器,如果map中沒有要查找的數(shù)據(jù),它返回的迭代器等于end函數(shù)返回的迭代器,程序說(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)判定數(shù)據(jù)是否出現(xiàn),是顯得笨了點(diǎn),但是,我打算在這里講解
Lower_bound函數(shù)用法,這個(gè)函數(shù)用來(lái)返回要查找關(guān)鍵字的下界(是一個(gè)迭代器)
Upper_bound函數(shù)用法,這個(gè)函數(shù)用來(lái)返回要查找關(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è)迭代器相等的話,則說(shuō)明map中不出現(xiàn)這個(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. 數(shù)據(jù)的清空與判空
清空map中的數(shù)據(jù)可以用clear()函數(shù),判定map中是否有數(shù)據(jù)可以用empty()函數(shù),它返回true則說(shuō)明是空map
7. 數(shù)據(jù)的刪除
這里要用到erase函數(shù),它有三個(gè)重載了的函數(shù),下面在例子中詳細(xì)說(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”));
//如果你要演示輸出效果,請(qǐng)選擇以下的一種,你看到的效果會(huì)比較好
//如果要?jiǎng)h除1,用迭代器刪除
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);
//如果要?jiǎng)h除1,用關(guān)鍵字刪除
Int n = mapStudent.erase(1);//如果刪除了會(huì)返回1,否則返回0
//用迭代器,成片的刪除
//一下代碼把整個(gè)map清空
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//成片刪除要注意的是,也是STL的特性,刪除區(qū)間是一個(gè)前閉后開的集合
//自個(gè)加上遍歷代碼,打印輸出吧
}
8. 其他一些函數(shù)用法
這里有swap,key_comp,value_comp,get_allocator等函數(shù),感覺到這些函數(shù)在編程用的不是很多,略過(guò)不表,有興趣的話可以自個(gè)研究
9. 排序
這里要講的是一點(diǎn)比較高深的用法了,排序問(wèn)題,STL中默認(rèn)是采用小于號(hào)來(lái)排序的,以上代碼在排序上是不存在任何問(wèn)題的,因?yàn)樯厦娴年P(guān)鍵字是int 型,它本身支持小于號(hào)運(yùn)算,在一些特殊情況,比如關(guān)鍵字是一個(gè)結(jié)構(gòu)體,涉及到排序就會(huì)出現(xiàn)問(wèn)題,因?yàn)樗鼪]有小于號(hào)操作,insert等函數(shù)在編譯的時(shí)候過(guò) 不去,下面給出兩個(gè)方法解決這個(gè)問(wèn)題
第一種:小于號(hào)重載,程序舉例
#include <map>
#include <string>
Using namespace std;
Typedef struct tagStudentInfo
{
Int nID;
String strName;
}StudentInfo, *PStudentInfo; //學(xué)生信息
Int main()
{
int nSize;
//用學(xué)生信息映射分?jǐn)?shù)
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ò)的,只要重載小于號(hào),就OK了,如下:
Typedef struct tagStudentInfo
{
Int nID;
String strName;
Bool operator < (tagStudentInfo const& _A) const
{
//這個(gè)函數(shù)指定排序策略,按nID排序,如果nID相等的話,按strName排序
If(nID < _A.nID) return true;
If(nID == _A.nID) return strName.compare(_A.strName) < 0;
Return false;
}
}StudentInfo, *PStudentInfo; //學(xué)生信息
第二種:仿函數(shù)的應(yīng)用,這個(gè)時(shí)候結(jié)構(gòu)體中沒有直接的小于號(hào)重載,程序說(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é)生信息映射分?jǐn)?shù)
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è)統(tǒng)一的整體,map的很多用法都和STL中其它的東西結(jié)合在一起,比如在排序上,這里默認(rèn)用的是小于號(hào),即less<>,如果要從大到小排序呢,這里涉及到的東西很多,在此無(wú)法一一加以說(shuō)明。
還要說(shuō)明的是,map中由于它內(nèi)部有序,由紅黑樹保證,因此很多函數(shù)執(zhí)行的時(shí)間復(fù)雜度都是log2N的,如果用map函數(shù)可以實(shí)現(xiàn)的功能,而STL Algorithm也可以完成該功能,建議用map自帶函數(shù),效率高一些。
下面說(shuō)下,map在空間上的特性,否則,估計(jì)你用起來(lái)會(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)存了吧,不說(shuō)了……