Map(STL關聯容器)

Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱為該關鍵字的值)的數據處理能力,由於這個特性,它完成有可能在我們處理一對一數據的時候,在編程上提供快速通道。

基本介紹

  • 外文名:Map
  • 性質:STL的一個關聯容器
  • 作用:提供一對一的數據處理能力
  • 插入方式:用數組方式插入數據等
簡介,好處,

簡介

這裡說下map內部數據的組織,map內部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),這顆樹具有對數據自動排序的功能,所以在map內部所有的數據都是有序的,後邊我們會見識到有序的好處。

好處

1. map的構造函式
map<int,string> maphai;
map<char,int> maphai;
map<string,char> mapstring;
map<string,int> mapstring;
map<int,char>mapint;
map<char,string>mapchar;
2. 數據的插入
在構造map容器後,我們就可以往裡面插入數據了。這裡講三種插入數據的方法:
第一種:用insert函式插入pair數據,
#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<<endl;
}
return 0;
}
第二種:用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<<endl;
}
}
第三種:用數組方式插入數據,下面舉例說明
#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<<endl;
}
}
以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完全一樣的,用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<<endl;
}
}
大家可以用如下程式,看下用數組插入在數據覆蓋上的效果
#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<<endl;
}
}
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<<endl;
}
}
第三種:用數組方式,程式說明如下
#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<<endl;
}
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>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
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"));
map<int,string>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
//如果你要演示輸出效果,請選擇以下的一種,你看到的效果會比較好
//如果要刪除1,用疊代器刪除
map<int,string>::iterator iter1;
iter1 = mapStudent.find(1);
mapStudent.erase(iter);
//如果要刪除1,用關鍵字刪除
Int n = mapStudent.erase(1);//如果刪除了會返回1,否則返回0
//用疊代器,成片的刪除
//一下代碼把整個map清空
mapStudent.erase(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
{
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));
}

相關詞條

熱門詞條

聯絡我們