STL之容器map
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-09-09 08:37:48 閱讀次數(shù):3235次
1.首先介紹map具有與set集合一樣的自動(dòng)排序功能
插入方法之pair<>
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> m; //必須有兩個(gè)類(lèi)型
m.insert(pair<int,string>(2,"student_two"));
m.insert(pair<int,string>(1,"student_one"));
m.insert(pair<int,string>(3,"student_three"));
map<int,string>::iterator it;
for (it = m.begin();it != m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl; //first與second分別指的是map<int,string>中的int和string
}
return 0;
//輸出
//1 student_one
// 2 student_two
// 3 student_three
// Press any key to continue
}
2.插入之value_type
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> m; //必須有兩個(gè)類(lèi)型
m.insert(map<int,string>::value_type (2,"student_two"));
m.insert(map<int,string>::value_type (1,"student_one"));
m.insert(map<int,string>::value_type (3,"student_three"));
map<int,string>::iterator it;
for (it = m.begin();it != m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl; //first與second分別指的是map<int,string>中的int和string
}
return 0;
//輸出
//1 student_one
// 2 student_two
// 3 student_three
// Press any key to continue
}
3.插入之?dāng)?shù)組
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> m; //必須有兩個(gè)類(lèi)型
m[2] = "student_two";
m[1] = "student_one";
m[3] = "student_three";
// m.insert(map<int,string>::value_type (2,"student_two"));
// m.insert(map<int,string>::value_type (1,"student_one"));
// m.insert(map<int,string>::value_type (3,"student_three"));
map<int,string>::iterator it;
for (it = m.begin();it != m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl; //first與second分別指的是map<int,string>中的int和string
}
return 0;
//輸出
//1 student_one
// 2 student_two
// 3 student_three
// Press any key to continue
}
以上3種方法實(shí)際上是有區(qū)分的,第1種和第2種沒(méi)有區(qū)分,第3種的數(shù)組有區(qū)分。在map數(shù)據(jù)的插入上觸及到唯1性的概念,即當(dāng)map中有這個(gè)關(guān)鍵字時(shí),insert是插入不了重復(fù)的數(shù)據(jù)的,就像之前的set1樣,但是數(shù)組可以插入,但是會(huì)覆蓋掉之前對(duì)應(yīng)的關(guān)鍵字的值,下面用
程序來(lái)講明
m.insert(map<int,string>::value_type (1,"student_two"));
m.insert(map<int,string>::value_type (1,"student_one"));
上面兩條語(yǔ)句履行后,map中的1這個(gè)關(guān)鍵字對(duì)應(yīng)的值是student_two,第2條語(yǔ)句并未生效,所以我們需要知道第2條語(yǔ)句有沒(méi)有插入成功:
pair<map<int,string>::iterator,bool>insert_type;
insert_type = m.insert(map<int,string>::value_type(1,"student_one"));
我們通過(guò)pair的第2個(gè)變量bool來(lái)判斷數(shù)據(jù)是不是插入成功,當(dāng)插入成功后,insert_type.second應(yīng)當(dāng)是true,反之為false。
下面給出代碼來(lái)演示:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> m; //必須有兩個(gè)類(lèi)型
pair<map<int,string>::iterator,bool>insert_type;
insert_type = m.insert(map<int,string>::value_type(1,"student_one"));
if(insert_type.second == true)
{
cout<<"insert sucessfully!"<<endl;
}
else
{
cout<<"fail!"<<endl;
}
insert_type = m.insert(map<int,string>::value_type(2,"student_two"));
if(insert_type.second == true)
{
cout<<"insert sucessfully!"<<endl;
}
else
{
cout<<"fail!"<<endl;
}
insert_type = m.insert(map<int,string>::value_type(1,"student_three"));
if(insert_type.second == true)
{
cout<<"insert sucessfully!"<<endl;
}
else
{
cout<<"fail!"<<endl;
}
map<int,string>::iterator it;
for (it = m.begin();it != m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl; //first與second分別指的是map<int,string>中的int和string
}
return 0;
}
2.用數(shù)組重復(fù)插入
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> m; //必須有兩個(gè)類(lèi)型
m[1] = "student_one";
m[1] = "student_two";
m[2] = "student_three";
map<int,string>::iterator it;
for (it = m.begin();it != m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl; //first與second分別指的是map<int,string>中的int和string
}
return 0;
}
數(shù)據(jù)的查找
1.count函數(shù)查找 缺點(diǎn):沒(méi)法返回所要查找的數(shù)據(jù)的位置
由于map中的數(shù)據(jù)都是1對(duì)1的映照關(guān)系,所以count的返回值只有2個(gè),0,1。
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> m; //必須有兩個(gè)類(lèi)型
<span style="white-space:pre"> </span>m[1] = "student_one";
<span style="white-space:pre"> </span>m[2] = "student_two";
<span style="white-space:pre"> </span>m[3] = "student_three";
<span style="white-space:pre"> </span>if(m.count(2)) cout<<"find it"<<endl;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>cout<<"not find"<<endl;
<span style="white-space:pre"> </span>if(m.count(4)) cout<<"find it"<<endl;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>cout<<"not find"<<endl;
<span style="white-space:pre"> </span>map<int,string>::iterator it;
<span style="white-space:pre"> </span>for (it = m.begin();it != m.end();it++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout<<it->first<<" "<<it->second<<endl; //first與second分別指的是map<int,string>中的int和string
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}
第2種:用find。 優(yōu)點(diǎn):可以返回要查詢(xún)的數(shù)據(jù)的迭代位置
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int,string> m; //必須有兩個(gè)類(lèi)型
m[1] = "student_one";
m[2] = "student_two";
m[3] = "student_three";
map<int,string>::iterator it;
it = m.find(2);
if(it != m.end())
cout<<it->first<<" "<<it->second<<endl;
else
cout<<"not find"<<endl;
it = m.find(4);
if(it != m.end())
cout<<it->first<<" "<<it->second<<endl;
else
cout<<"not find"<<endl;
return 0;
}
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)