版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_34018840/article/details/89093275
1、map简介
map是STL的一类关联式容器,提供一对一(其中第一个称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,其特点是增加和删除节点对迭代器的影响很小,除了操作节点,对其他的节点都没有什么影响。map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以map内部所有的数据都是有序的。
2、map的声明与构造
- #include <map> //注意,STL头文件没有扩展名.h。
- map对象是模板类,需要关键字和存储对象两个模板参数:std:map<int,string> personnel;
map常用构造函数:
map<int, string> mapStudent;
3、数据插入
- 用insert函数插入pair数据
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
- 用数组方式插入数据
mapStudent[1] = "student_one";
mapStudent[2] = "student_two";
需要注意的是:用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对 应的值,用程序说明:下面两条语句执行后,map中关键字1对应的值是“student_one”,第二条语句并没有生效。
mapStudent.insert(map<int, string> (1, "student_one"));
mapStudent.insert(map<int, string> (1, "student_two"));
4、map的大小
map里面插入了数据,可以用size函数获取已插入数据的大小,用法如下:
int nSize = mapStudent.size();
5、map中数据的遍历
- 应用前向迭代器
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[2] = "student_two";
map<int, string>::iterator iter; //前向迭代器
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
}
- 应用反向迭代器
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
map<int, string>::reverse_iterator iter; //反向迭代器
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
cout<<iter->first<<" "<<iter->second<<endl;
}
- 应用数组的形式
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
int nSize = mapStudent.size();
//此处应注意,应该是 for(int nindex = 1; nindex <= nSize; nindex++)
//而不是 for(int nindex = 0; nindex < nSize; nindex++)
for(int nindex = 1; nindex <= nSize; nindex++)
cout<<mapStudent[nindex]<<endl;
}
6、map元素的查找
- 第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置。由于map的关键字只能出现一次,因此count函数的返回值只有两个,0或者1,出现时返回1。
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
if(mapStudent.count(1) > 0)
cout<<"Find, the value is "<<iter->second<<endl;
else
cout<<"Do not Find"<<endl;
- 第二种:用find函数来定位数据出现位置,传入参数key,返回一个迭代器。当数据出现时,返回数据所在位置的迭代器;否则返回的迭代器等于end函数返回的迭代器。begin()和end()两个成员分别代表map对象中第一个和最后一个条目,其类型是iterator。
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
cout<<"Find, the value is "<<iter->second<<endl;
else
cout<<"Do not Find"<<endl;
7、map元素的删除
移除某个map中某个条目用erase()该成员方法的定义如下:
- iterator erase (iterator it); //通过一个条目对象删除
- iterator erase (iterator first,iterator last); //删除一个范围
- size_type erase (const Key&key); //通过关键字删除
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
//用迭代器删除删除1
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);
//用关键字删除1
int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
//用迭代器成片删除,清空map
mapStudent.erase( mapStudent.begin(), mapStudent.end() );