加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

STL之容器map

发布时间:2020-12-13 20:47:45 所属栏目:PHP教程 来源:网络整理
导读:1.首先介绍map具有与set集合一样的自动排序功能 插入方法之pair #include map#include string#include iostreamusing namespace std;int main(){ mapint,string m; //必须有两个类型 m.insert(pairint,string(2,student_two));m.insert(pairint,string(1,stu

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,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,"student_two")); // m.insert(map<int,"student_one")); // m.insert(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,"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,"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,"student_three")); if(insert_type.second == true) { cout<<"insert sucessfully!"<<endl; } else { cout<<"fail!"<<endl; } 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>中的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; }



(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读