一、關(guān)于map的介紹
map是STL的一個(gè)容器,和set一樣,map也是一種關(guān)聯(lián)式容器。它提供一對(duì)一(其中第一個(gè)可以稱(chēng)為關(guān)鍵字,每個(gè)關(guān)鍵字只能在map中出現(xiàn)一次,第二個(gè)可能稱(chēng)為該關(guān)鍵字的值)的數(shù)據(jù)處理能力,由于這個(gè)特性,有助于我們處理一對(duì)一數(shù)據(jù)。這里說(shuō)下map內(nèi)部數(shù)據(jù)的組織,map內(nèi)部是自建一顆紅黑樹(shù)(一種非嚴(yán)格意義上的平衡二叉樹(shù)),這顆樹(shù)具有對(duì)數(shù)據(jù)自動(dòng)排序的功能,所以在map內(nèi)部所有的數(shù)據(jù)都是有序的。學(xué)習(xí)map我們一定要理解什么是一對(duì)一的數(shù)據(jù)映射?比如:一個(gè)班級(jí)中,每個(gè)學(xué)生的學(xué)號(hào)跟他的姓名就存在著一一映射的關(guān)系,這個(gè)模型用map可能輕易描述,很明顯學(xué)號(hào)用int 描述,姓名用字符串描述采用的string,于是我們使用的map形式如下:map<int , string> student;
這里說(shuō)一下map和set容器的區(qū)別。
對(duì)于map中的每個(gè)節(jié)點(diǎn)存儲(chǔ)的是一對(duì)信息,包括一個(gè)鍵和一個(gè)值,各個(gè)節(jié)點(diǎn)之間的鍵值不能重復(fù)。
對(duì)于set中的每個(gè)節(jié)點(diǎn)存儲(chǔ)的是一個(gè)信息,只有一個(gè)鍵,但是每個(gè)鍵值也是唯一的。set表示的是集合的概念。
對(duì)于map的學(xué)習(xí),或者說(shuō)是對(duì)STL中的容器的學(xué)習(xí),要知道每種容器的實(shí)現(xiàn)原理,每種適合適合解決什么問(wèn)題的,才是關(guān)鍵~~~~
二、map中常用的操作
2.1 map中的構(gòu)造函數(shù)
map(const map& m) // 拷貝構(gòu)造函數(shù)
map(iterator begin, iterator end ); //區(qū)間構(gòu)造函數(shù)
map(iterator begin, iterator end, const traits& _compare) //帶比較謂詞的構(gòu)造函數(shù)
map(iterator begin, iterator end, const traits& _compare, const allocator& all) //帶分配器
2.2 map中的一些基礎(chǔ)函數(shù)
begin,end,rbegin,rend,empty,clear,size,max_size。八個(gè)常用的函數(shù),看到名字應(yīng)該就知道怎么用了吧,看看代碼:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> studentMessage;
map<int,string>::iterator iter;
studentMessage.insert(pair<int , string>(54090101,"Mike"));
studentMessage.insert(pair<int , string>(54090102,"Sam"));
studentMessage.insert(pair<int , string>(54090103,"Jake"));
//begin獲取map中的第一個(gè)元素的迭代器,并且等于rend
//end獲取map中的最后一個(gè)元素下一位置的迭代器,并且等于rbegin
cout<<"迭代器中的元素如下:"<<endl;
for(iter = studentMessage.begin() ; iter != studentMessage.end() ; ++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
//看看max_size和size的值得意義
cout<<"map 的 max_size 的值:"<<studentMessage.max_size()<<endl;
cout<<"map 的 size 的值:"<<studentMessage.size()<<endl;
//看看empty和clear的使用
studentMessage.clear();
if(studentMessage.empty())
{
cout<<"The map is Empty !!"<<endl;
}
else
{
cout<<"The map is not Empty !!"<<endl;
}
return 0;
}
2.3 map中的的查找元素
map中用來(lái)查找的函數(shù)是find,但是能完成查找功能的函數(shù)卻并不止這一個(gè),比如count也是可以完成查找的,因?yàn)閙ap中的鍵值是不允許重復(fù)的,所以一個(gè)鍵值只能出現(xiàn)一次,這說(shuō)明count的返回值就只能是0或1了,那么顯然這就能完成查找了,但是用count來(lái)完成查找并不是最優(yōu)的選擇,因?yàn)樵瓉?lái)的本意是用count來(lái)完成計(jì)數(shù)的,這在vector等序列式容器中是灰常好用的,而map中之所以有這個(gè)count函數(shù),就是為了STL提供統(tǒng)一的接口,這樣說(shuō)來(lái)map中的upper_bound和lower_bound,equel_range等函數(shù)組合起來(lái)也是可以完成查找功能的(想一想怎么實(shí)現(xiàn))。這里有個(gè)疑問(wèn):count和find對(duì)于完成的效率是不是一致的呢??
我們分別看看分別用find和count來(lái)完成查找:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<int,string> studentMessage;
studentMessage.insert(map<int,string>::value_type(54090101,"Mike"));
studentMessage.insert(map<int,string>::value_type(54090102,"Sam"));
studentMessage.insert(map<int,string>::value_type(54090103,"Jake"));
if(studentMessage.find(54090101) != studentMessage.end())
{
cout<<"find success !!"<<endl;
}
if(studentMessage.count(54090101))
{
cout<<"count success !!"<<endl;
}
return 0;
}
看到了嗎,count和find還是有區(qū)別的,那就是count只能單純的查找元素是否存在,而find能定位要查找元素的位置。有一點(diǎn)需要注意的是查找的參數(shù)是鍵值哦!!
2.4 map中數(shù)據(jù)的插入和刪除
無(wú)論是對(duì)于哪個(gè)容器,插入和刪除都是非常重要的操作,先說(shuō)一說(shuō)map中數(shù)據(jù)的插入,數(shù)據(jù)的插入大概有三種方式,第一種:insert(pair<T1,T2,>(key1,value1))。第二種:insert(map<T1,T2>::value_type(key1,value1)),這種插入方式和第一種基本相似。第三種:利用數(shù)組進(jìn)行插入,這個(gè)一會(huì)用程序演示吧。
關(guān)于數(shù)據(jù)的刪除,大概有三種方式進(jìn)行刪除:第一種:erase(map<T1,T2>::iterator iter),刪除迭代器所指的節(jié)點(diǎn)。第二種:erase(key k),根據(jù)鍵值進(jìn)行刪除,刪除鍵值k所指的節(jié)點(diǎn) 。第三種:erase(map<T1,T2>::iteratormap iter1,<T1,T2>::iteratoriter2),刪除iter1和iter2之間的數(shù)據(jù)。
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
/*
map<int,string> tmp;
map<int,string>::const_iterator iter1,iter2;
tmp.insert(pair<int,string>(54090104,"Bob"));
tmp.insert(pair<int,string>(54090105,"Ben"));
iter1 = tmp.begin();
iter2 = tmp.end();
*/
map<int,string> studentMessage;
map<int,string>::iterator iter;
//向map中插入數(shù)據(jù)
studentMessage.insert(pair<int,string>(54090101,"Mike"));
studentMessage.insert(pair<int,string>(54090101,"MIKE"));//重復(fù)插入
studentMessage.insert(map<int,string>::value_type(54090102,"Sam"));
studentMessage.insert(map<int,string>::value_type(54090102,"SAM"));//重復(fù)插入
studentMessage[54090103] = "Jake";
studentMessage[54090103] = "JAKE";//重復(fù)插入
//為了測(cè)試刪除,先插入兩個(gè)數(shù)據(jù),看插入結(jié)果主要看上面的插入方式
studentMessage[54090104] = "Bob";
studentMessage[54090105] = "Ben";
cout<<"完成插入后map中的數(shù)據(jù):"<<endl;
for(iter = studentMessage.begin() ; iter != studentMessage.end() ; ++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
//從map中刪除數(shù)據(jù)
iter = studentMessage.begin();
studentMessage.erase(iter);
cout<<"利用迭代器刪除map中第一個(gè)元素:"<<endl;
for(iter = studentMessage.begin() ; iter != studentMessage.end() ; ++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
studentMessage.erase(54090102);
cout<<"利用鍵值刪除map中的第一個(gè)元素:"<<endl;
for(iter = studentMessage.begin() ; iter != studentMessage.end() ; ++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
studentMessage.erase(studentMessage.begin(),studentMessage.end());
cout<<"利用范圍迭代器刪除map中的所有數(shù)據(jù):"<<endl;
for(iter = studentMessage.begin() ; iter != studentMessage.end() ; ++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
注意:通過(guò)觀察輸出結(jié)果,利用數(shù)組進(jìn)行插入對(duì)數(shù)據(jù)進(jìn)行了覆蓋,而其他兩種插入方式?jīng)]有進(jìn)行覆蓋,實(shí)際上屬于插入失敗,還要注意的是,利用數(shù)組進(jìn)行插入下標(biāo)實(shí)際上是鍵值。
2.5 其他一些常用的函數(shù)或運(yùn)算符
比如swap和key_comp函數(shù),還有操作符:==,!=,<,<=,>,>=等,對(duì)于==運(yùn)算符,只有兩個(gè)map中所有的元素完全一致,才說(shuō)兩個(gè)map相等,而<,<=,>,>=起著決定作用的是兩個(gè)map第一個(gè)不同的元素,這和string庫(kù)中的strcmp相似。這些東西就不多說(shuō)了。。
新聞熱點(diǎn)
疑難解答
圖片精選