国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > STL之容器map

STL之容器map

来源:程序员人生   发布时间:2015-09-09 08:37:48 阅读次数:2971次

1.首先介绍map具有与set集合一样的自动排序功能

  插入方法之pair<>

#include <map> #include <string> #include <iostream> using namespace std; int main() { map<int,string> m; //必须有两个类型 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; //必须有两个类型 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.插入之数组

#include <map> #include <string> #include <iostream> using namespace std; int main() { map<int,string> m; //必须有两个类型 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种方法实际上是有区分的,第1种和第2种没有区分,第3种的数组有区分。在map数据的插入上触及到唯1性的概念,即当map中有这个关键字时,insert是插入不了重复的数据的,就像之前的set1样,但是数组可以插入,但是会覆盖掉之前对应的关键字的值,下面用 程序来讲明
m.insert(map<int,string>::value_type (1,"student_two")); m.insert(map<int,string>::value_type (1,"student_one"));
上面两条语句履行后,map中的1这个关键字对应的值是student_two,第2条语句并未生效,所以我们需要知道第2条语句有没有插入成功:

pair<map<int,string>::iterator,bool>insert_type; insert_type = m.insert(map<int,string>::value_type(1,"student_one"));
我们通过pair的第2个变量bool来判断数据是不是插入成功,当插入成功后,insert_type.second应当是true,反之为false。
下面给出代码来演示:

#include <map> #include <string> #include <iostream> using namespace std; int main() { map<int,string> m; //必须有两个类型 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.用数组重复插入

#include <map> #include <string> #include <iostream> using namespace std; int main() { map<int,string> m; //必须有两个类型 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; }


   数据的查找

1.count函数查找              缺点:没法返回所要查找的数据的位置

  由于map中的数据都是1对1的映照关系,所以count的返回值只有2个,0,1。

#include <map> #include <string> #include <iostream> using namespace std; int main() {     map<int,string> m; //必须有两个类型  <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。     优点:可以返回要查询的数据的迭代位置

#include <map> #include <string> #include <iostream> using namespace std; int main() { map<int,string> m; //必须有两个类型 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; }


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生