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
}
#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
}
#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
}
以上三种方法其实是有区别的,第一种和第二种没有区别,第三种的数组有区别。
在map数据的插入上涉及到唯一性的概念,即当map中有这个关键字时,insert是插入不了重复的数据的,就像之前的set一样,然而数组可以插入,但是会覆盖掉之前对应的关键字的值,下面用 程序来说明
m.insert(map<int,string>::value_type (1,"student_two"));
m.insert(map<int,string>::value_type (1,"student_one"));
上面两条语句执行后,map中的1这个关键字对应的值是student_two,第二条语句并未生效,所以我们需要知道第二条语句有无插入成功:
pair<map<int,string>::iterator,bool>insert_type;
insert_type = m.insert(map<int,string>::value_type(1,"student_one"));
我们通过pair的第二个变量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中的数据都是一对一的映射关系,所以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;
}
第二种:用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;
}