您的位置:首页 > 其它

STL---map用法详解

2015-11-03 21:04 549 查看
一.Map概述

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力。

这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序!!!的功能,所以在map内部所有的数据都是有序的!!!,后边我们会见识到有序的好处。

下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:

map<int, string> mapStudent;


示例:

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<string, string> mp;
//增加。。。
mp["岳不群"]="华山派掌门人,人称君子剑";
mp["张三丰"]="武当掌门人,太极拳创始人";
mp["东方不败"]="第一高手,葵花宝典";
mp["阿里马云"]="风清扬";

//查找。。
//if(mp.find("岳不群") != mp.end()){
//     cout<<mp["岳不群"]<<endl;
//}

map<string, string>::iterator it;
for(it = mp.begin();it != mp.end(); ++it)
{//输出的方式是排序之后的
cout<<it->first<<":"<<it->second<<endl;
}
return 0;
}


结果:

阿里马云:风清扬
东方不败:第一高手,葵花宝典
岳不群:华山派掌门人,人称君子剑
张三丰:武当掌门人,太极拳创始人

Process returned 0 (0x0)   execution time : 0.717 s
Press any key to continue.


二、map的构造函数

map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:

map<int, string> mapStudent;


三、数据的插入

在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法。

1、用insert函数插入pair数据。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(2, "stu_two"));
mapStudent.insert(pair<int, string>(1, "stu_one"));
mapStudent.insert(pair<int, string>(3, "stu_three"));

map<int, string>::iterator it;
for(it = mapStudent.begin();it != mapStudent.end(); it++)
{//内部自动排序,有序输出
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


结果:

1 stu_one
2 stu_two
3 stu_three

Process returned 0 (0x0)   execution time : 0.304 s
Press any key to continue.


2、用insert函数插入value_type数据。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type(2, "stu_two"));
mapStudent.insert(map<int, string>::value_type(1, "stu_one"));
mapStudent.insert(map<int, string>::value_type(3, "stu_three"));

map<int, string>::iterator it;
for(it = mapStudent.begin();it != mapStudent.end(); it++)
{//内部自动排序,有序输出
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


3、用数组方式插入数据。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
//key-value对应,与插入顺序无关,内部自动排序。
mapStudent[2] = "stu_two";
mapStudent[1] = "stu_one";
mapStudent[3] = "stu_three";

map<int, string>::iterator it;
for(it = mapStudent.begin();it != mapStudent.end(); it++)
{//内部自动排序,有序输出
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


4、分析

以上三种用法,都可以实现数据的插入,但有区别。第一种和第二种在效果上完全一样,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的。

但是用数组方式就不同了,它可以覆盖以前该关键字对应的值

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type(2, "stu_two"));
mapStudent.insert(map<int, string>::value_type(1, "stu_one"));
mapStudent.insert(map<int, string>::value_type(1, "stu_three"));
mapStudent.insert(map<int, string>::value_type(3, "stu_one"));

map<int, string>::iterator it;
for(it = mapStudent.begin();it != mapStudent.end(); it++)
{//内部自动排序,有序输出
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


结果:

1 stu_one
2 stu_two
3 stu_one

Process returned 0 (0x0)   execution time : 0.404 s
Press any key to continue.


分析以上代码结果可知:同一个关键字(key),只能对应一个value值,若要给他用insert函数插入数据时,插入其他value值,则插入不成功;同一个value值可以对应到多个关键字。

怎么知道insert语句是否插入成功的问题,可以用pair来获得是否插入成功,程序如下

pair<map<int, string>::iterator, bool> insert_pair;
insert_pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));


我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话insert_pair.second应该是true的,否则为false。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
pair<map<int, string>::iterator, bool> insert_pair;

insert_pair = mapStudent.insert(pair<int, string>(1, "stu_one"));

if(insert_pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
insert_pair = mapStudent.insert(pair<int, string>(1, "stu_two"));
if(insert_pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}

map<int, string>::iterator it;
for(it = mapStudent.begin();it != mapStudent.end(); it++)
{//内部自动排序,有序输出
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


结果:

Insert Successfully
Insert Failure
1 stu_one

Process returned 0 (0x0)   execution time : 0.408 s
Press any key to continue.


用数组插入在数据覆盖上的效果:

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
mapStudent[1] = "stu_one";
mapStudent[1] = "stu_two";
mapStudent[2] = "stu_three";

map<int, string>::iterator it;
for(it = mapStudent.begin();it != mapStudent.end(); it++)
{//内部自动排序,有序输出
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


结果:

1 stu_two
2 stu_three

Process returned 0 (0x0)   execution time : 1.341 s
Press any key to continue.


有上述代码结果可知:使用数组插入数据,对同一关键字(key)插入不同数据时,可实现数据覆盖!!!

四、map的大小

在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:

int nSize = mapStudent.size();


五、数据的遍历

这里也提供三种方法,对map进行遍历 。

1、应用前向迭代器

上面举例程序中到处都是了,略过不表 。

2、应用反相迭代器

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type(2, "stu_two"));
mapStudent.insert(map<int, string>::value_type(1, "stu_one"));
mapStudent.insert(map<int, string>::value_type(3, "stu_three"));

//反向迭代器
map<int, string>::reverse_iterator it;
for(it = mapStudent.rbegin();it != mapStudent.rend(); it++)
{//内部自动排序,有序输出(与正向的结果相反)
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


结果:

3 stu_three
2 stu_two
1 stu_one

Process returned 0 (0x0)   execution time : 0.431 s
Press any key to continue.


3、用数组方式(慎用)

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type(2, "stu_two"));
mapStudent.insert(map<int, string>::value_type(4, "stu_four"));
mapStudent.insert(map<int, string>::value_type(3, "stu_three"));

//数组形式遍历:数组下标要和key值一一对应!!!
int nSize = mapStudent.size();
cout<<"size = "<<nSize<<endl;
cout<<"下标不对应:"<<endl;
for(int i=1; i<=nSize; i++)
{
cout<<mapStudent[i]<<endl;
}
cout<<"下标对应:"<<endl;
for(int i=2; i<=nSize+1; i++)
{
cout<<mapStudent[i]<<endl;
}
return 0;
}


结果:

size = 3
下标不对应:

stu_two
stu_three
下标对应:
stu_two
stu_three
stu_four

Process returned 0 (0x0)   execution time : 0.419 s
Press any key to continue.


六、数据的查找(包括判定这个关键字是否在map中出现)

在这里我们将体会,map在数据插入时保证有序的好处。要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的map基本用法。这里给出两种种数据查找方法 。

1、用count函数来判定关键字是否出现

其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了

2、用find函数来定位数据出现位置

它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type(1, "stu_one"));
mapStudent.insert(map<int, string>::value_type(2, "stu_two"));
mapStudent.insert(map<int, string>::value_type(3, "stu_three"));

map<int, string>::iterator it;
it = mapStudent.find(1);
if(it != mapStudent.end())
{
cout<<"Find, the value is: "<<it->second<<endl;
}
else
{
cout<<"don't find !"<<endl;
}
return 0;
}


结果:

Find the value is: stu_one

Process returned 0 (0x0)   execution time : 3.655 s
Press any key to continue.


七、排序

排序问题,STL中默认是采用小于号来排序的,以上代码在排序上是不存在任何问题的,因为上面的关键字是int型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题 。

1、小于号重载

#include<iostream>
#include<map>
#include<string>
using namespace std;

typedef struct StuInfo
{
int id;
string name;
}StuInfo, *PStuInfo;

int main()
{
//用学生信息映射分数
map<StuInfo, int> mapStudent;
StuInfo stuInfo;//!!!
//插入数据
stuInfo.id = 1;
stuInfo.name = "stu_one";
mapStudent.insert(pair<StuInfo, int>(stuInfo, 90));

stuInfo.id = 2;
stuInfo.name = "stu_two";
mapStudent.insert(pair<StuInfo, int>(stuInfo, 80));

return 0;
}


以上程序是无法编译通过的,只要重载小于号,就OK了,如下:

#include<iostream>
#include<map>
#include<string>
using namespace std;

typedef struct StuInfo
{
int id;
string name;
//重载"<"运算符!!!
bool operator < (StuInfo const& s)const
{
//这个函数指定排序策略,按id排序,如果id相等的话,按strName排序
if(id < s.id) return true;
if(id == s.id) return name.compare(s.name)<0;
return false;
}
}StuInfo, *PStuInfo;

int main()
{
//用学生信息映射分数
map<StuInfo, int> mapStudent;
StuInfo stuInfo;//!!!
//插入数据
stuInfo.id = 1;
stuInfo.name = "stu_one";
mapStudent.insert(pair<StuInfo, int>(stuInfo, 90));

stuInfo.id = 2;
stuInfo.name = "stu_two";
mapStudent.insert(pair<StuInfo, int>(stuInfo, 80));

return 0;
}


2、仿函数的应用(这个时候结构体中没有直接的小于号重载)

#include<iostream>
#include<map>
#include<string>
using namespace std;

typedef struct StuInfo
{
int id;
string name;
}StuInfo, *PStuInfo;
//!!!!!!!!!!!!!!!!
class sort
{
public:
bool operator()(StuInfo const& _A, StuInfo const& _B)const
{
if(_A.id < _B.id) return true;
if(_A.id == _B.id) return _A.name.compare(_B.name)<0;
return false;
}
};

int main()
{
//用学生信息映射分数
map<StuInfo, int, sort> mapStudent;//!!!!!!!!!!!!!!!
StuInfo stuInfo;//!!!
//插入数据
stuInfo.id = 1;
stuInfo.name = "stu_one";
mapStudent.insert(pair<StuInfo, int>(stuInfo, 90));

stuInfo.id = 2;
stuInfo.name = "stu_two";
mapStudent.insert(pair<StuInfo, int>(stuInfo, 80));

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stl