日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > php教程 > STL之容器map

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)
程序員人生
------分隔線(xiàn)----------------------------
分享到:
------分隔線(xiàn)----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 日韩成人在线视频 | 美日韩在线观看 | 亚洲一区在线观看视频 | 国内视频一区 | 亚洲综合久久久 | 亚洲最大黄网 | 欧美精品乱码久久久久久按摩 | 精品一区二区三区免费视频 | 欧美日韩电影在线观看 | 国产一区二区三区在线 | 四色永久访问网站 | 夜夜爽爽爽久久久久久魔女 | 精品伊人久久久久7777人 | 91在线看看 | 欧美国产日韩视频 | 中文字幕av一区二区 | 无码日韩精品一区二区免费 | 国产iv一区二区三区 | 国产日韩精品久久 | 自拍偷拍欧美日韩 | 麻豆视频一区二区 | 草久久 | 免费国产在线视频 | 久久成人综合 | 日韩一区中文字幕 | 欧美精品在线观看 | 在线观看免费黄视频 | 国产一区视频网站 | 国产在线视频一区二区 | 中文字幕国产一区 | 精品一区二区精品 | 久久精品一区二区三区不卡牛牛 | 天堂精品一区二区三区 | 亚洲一区二三区 | 日韩国产 | 国产精品麻豆 | 亚洲视频影院 | 亚洲成人1区 | 成人福利在线观看 | 国产精品久久久久无码av | 天堂在线中文字幕 |