Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱爲關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱爲該關鍵字的值)的數據 處理能力,由於這個特性,它完成有可能在我們處理一對一數據的時候,在編程上提供快速通道。這裏說下map內部數據的組織,map內部自建一顆紅黑樹(一 種非嚴格意義上的平衡二叉樹),這顆樹具有對數據自動排序的功能,所以在map內部所有的數據都是有序的,後邊我們會見識到有序的好處。
map是一類關聯式容器。它的特點是增加和刪除節點對迭代器的影響很小,除了那個操作節點,對其他的節點都沒有什麼影響。
對於迭代器來說,可以修改實值,而不能修改key。
自動建立Key-value的對應。key 和 value可以是任意你需要的類型。
根據key值快速查找記錄,查找的複雜度基本是Log(N),如果有1000個記錄,最多查找10次,1,000,000個記錄,最多查找20次。
快速插入Key -Value 記錄。
快速刪除記錄
根據Key 修改value記錄。
遍歷所有記錄。
使用map得包含map類所在的頭文件
#include <map> //注意,STL頭文件沒有擴展名.h
map對象是模板類,需要關鍵字和存儲對象兩個模板參數:
std:map<int,string> personnel;
這樣就定義了一個用int作爲索引,並擁有相關聯的指向string的指針.
爲了使用方便,可以對模板類進行一下類型定義,
map共提供了6個構造函數,這塊涉及到內存分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這裏要說下的就是,我們通常用如下方法構造一個map:
在構造map容器後,我們就可以往裏面插入數據了。這裏講三種插入數據的方法:
第一種:用insert函數插入pair數據,下面舉例說明:
int testMap01()
{
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"));
for (map<int, string>::iterator iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout << iter->first << ' ' << iter->second << endl;
return 0;
}
第二種:用insert函數插入value_type數據,下面舉例說明
//第二種:用insert函數插入value_type數據,下面舉例說明
int testMap02()
{
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 << endl;
return 0;
}
第三種:用數組方式插入數據,下面舉例說明
int testMap03()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[2] = "student_two";
mapStudent[3] = "student_three";
for (map<int, string>::iterator iter = mapStudent.begin(); iter != mapStudent.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。
下面給出完成代碼,演示插入成功與否問題:
int testMapInsert04()
{
map<int, string> mapStudent;
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));//插入數據後會返回一個Insert_Pair迭代器
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 << endl;
return 0;
}
大家可以用如下程序,看下用數組插入在數據覆蓋上的效果:
//驗證數組形式插入數據的效果
int testInserArray05()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[1] = "student_two";
mapStudent[2] = "student_three"; //會覆蓋之前的數據
for (map<int, string>::iterator iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout << iter->first << ' ' << iter->second << endl;
int nSize = mapStudent.size();
cout << "nSize = " << nSize << endl;
return 0;
}
在往map裏面插入了數據,我們怎麼知道當前已經插入了多少數據呢,可以用size函數,用法如下:
Int nSize = mapStudent.size();
這裏也提供三種方法,對map進行遍歷
第一種:應用前向迭代器,上面舉例程序中到處都是了,略過不表
第二種:應用反相迭代器,下面舉例說明,要體會效果,請自個動手運行程序
//第二種,利用反向迭代器
int testMap06()
{
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"));
for (map<int, string>::reverse_iterator iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
cout << iter->first << " " << iter->second << endl;
return 0;
}
第三種,用數組的形式,程序說明如下:
int testMap07()
{
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++)
//而不是 for(int nindex = 0; nindex < nSize; nindex++)
for (int nindex = 1; nindex <= nSize; nindex++)
cout << mapStudent[nindex] << endl;
return 0;
}
8、查找並獲取map中的元素(包括判定這個關鍵字是否在map中出現)
在這裏我們將體會,map在數據插入時保證有序的好處。
要判定一個數據(關鍵字)是否在map中出現的方法比較多,這裏標題雖然是數據的查找,在這裏將穿插着大量的map基本用法。
這裏給出三種數據查找方法
第一種:用count函數來判定關鍵字是否出現,其缺點是無法定位數據出現位置,由於map的特性,一對一的映射關係,就決定了count函數的返回值只有兩個,要麼是0,要麼是1,出現的情況,當然是返回1了
第二種:用find函數來定位數據出現位置,它返回的一個迭代器,當數據出現時,它返回數據所在位置的迭代器,如果map中沒有要查找的數據,它返回的迭代器等於end函數返回的迭代器。
查找map中是否包含某個關鍵字條目用find()方法,傳入的參數是要查找的key,在這裏需要提到的是begin()和end()兩個成員,
分別代表map對象中第一個條目和最後一個條目,這兩個數據的類型是iterator.
程序說明:
int testMapFind08()
{
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 = mapStudent.find(1);
if (iter != mapStudent.end())
cout << "Find, the value is " << iter->second << endl;
else
cout << "Do not Find" << endl;
return 0;
}
通過map對象的方法獲取的iterator數據類型是一個std::pair對象,包括兩個數據 iterator->first和 iterator->second分別代表關鍵字和存儲的數據。
第三種:這個方法用來判定數據是否出現,是顯得笨了點,但是,我打算在這裏講解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中不出現這個關鍵字,
程序說明:
int testMapbound09()
{
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(1);
//返回的是下界1的迭代器
cout << iter->second << endl;
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;
return 0;
}
移除某個map中某個條目用erase()
該成員方法的定義如下:
iterator erase(iterator it);//通過一個條目對象刪除
iterator erase(iterator first,iterator last)//刪除一個範圍
size_type erase(const Key&key);//通過關鍵字刪除
clear()就相當於
enumMap.erase(enumMap.begin(),enumMap.end());
這裏要用到erase函數,它有三個重載了的函數,下面在例子中詳細說明它們的用法:
int testEraseMap10()
{
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.erase(mapStudent.begin(), mapStudent.end());
return 0;
}
map中的swap不是一個容器中的元素交換,而是兩個容器所有元素的交換。
map中的元素是自動按Key升序排序,所以不能對map用sort函數;
這裏要講的是一點比較高深的用法了,排序問題,STL中默認是採用小於號來排序的,以上代碼在排序上是不存在任何問題的,因爲上面的關鍵字是int 型,它本身支持小於號運算,在一些特殊情況,比如關鍵字是一個結構體,涉及到排序就會出現問題,因爲它沒有小於號操作,insert等函數在編譯的時候過 不去,下面給出兩個方法解決這個問題:
第一種:小於號重載,程序舉例:
int testMapSort11()
{
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 << ' ' << iter->first.strName << ' ' << iter->second << endl;
return 0;
}
第二種:仿函數的應用,這個時候結構體中沒有直接的小於號重載,程序說明:
class 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 testFunction12()
{ //用學生信息映射分數
map<Studentinfo, int, sort>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 << ' ' << iter->first.strName << ' ' << iter->second << endl;
return 0;
}
由於STL是一個統一的整體,map的很多用法都和STL中其它的東西結合在一起,比如在排序上,這裏默認用的是小於號,即less<>,如果要從大到小排序呢,這裏涉及到的東西很多,在此無法一一加以說明。
還要說明的是,map中由於它內部有序,由紅黑樹保證,因此很多函數執行的時間複雜度都是log2N的,如果用map函數可以實現的功能,而STL Algorithm也可以完成該功能,建議用map自帶函數,效率高一些。
下面說下,map在空間上的特性,否則,估計你用起來會有時候表現的比較鬱悶,由於map的每個數據對應紅黑樹上的一個節點,這個節點在不保存你的 數據時佔用16個字節的,一個父節點指針,左右孩子指針,還有一個枚舉值(標示紅黑的,相當於平衡二叉樹中的平衡因子),我想大家應該知道,這些地方很費內存了吧,不說了……
C++ maps是一種關聯式容器,包含“關鍵字/值”對
#if 0
begin() 返回指向map頭部的迭代器
clear() 刪除所有元素
count() 返回指定元素出現的次數
empty() 如果map爲空則返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊條目的迭代器對
erase() 刪除一個元素
find() 查找一個元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比較元素key的函數
lower_bound() 返回鍵值 >= 給定元素的第一個位置
max_size() 返回可以容納的最大元素個數
rbegin() 返回一個指向map尾部的逆向迭代器
rend() 返回一個指向map頭部的逆向迭代器
size() 返回map中元素的個數
swap() 交換兩個map
upper_bound() 返回鍵值>給定元素的第一個位置
value_comp() 返回比較元素value的函數
#endif
int main(){
cout << "插入數據01" << endl;
testMap01();
cout << "插入數據02" << endl;
testMap02();
cout << "插入數據03" << endl;
testMap03();
cout << "驗證插入數據是否成功" << endl;
testMapInsert04();
cout << "驗證數組插入數據效果" << endl;
testInserArray05();
cout << "驗證迭代器遍歷map" << endl;
testMap06();
cout << "驗證數組遍歷map" << endl;
testMap07();
cout << "驗證map查找" << endl;
testMapFind08();
cout << "驗證map Bound" << endl;
testMapbound09();
cout << "驗證map erase" << endl;
testEraseMap10();
cout << "驗證map sort" << endl;
testMapSort11();
cout << "驗證map 仿函數 sort" << endl;
testFunction12();
return 0;
}
參考資料: