map是键---值对的集合,map类型通常可理解为关联数组:可使用键作为下标来获取一个值正如内置数组类型一样。而关联的本质在于元素的值与某个特定的键相关联,而并非通过元素在数组中的位置来获取
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
一:map的定义
#include<map>头文件
对象定义
map<k,v> m; | 创建一个名为m的空map对象,其键和值的类型分别为k和v |
map<k,v> m(m2); | 创建m2的副本m,m和m2必须有相同的键类型和值类型 |
map<k,v> m(b,e); | 创建map类型的对象m,存储迭代器b和e标记的范围内所有元素的副本,元素类型必须能转换成pair<const k,v>; |
键类型的约束:
对于键类型,唯一的约束就是必须支持<操作符,至于是否支持其他的关系或相等运算,则不作要求
map中的元素是自动按Key升序排序,所以不能对map用sort函数,以往都是写好用sort,这个不用的原因
STL中默认是采用小于号来排序的,比如关键字是int,string 型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去
可以如下解决:
#include<iostream>
#include<map>
using namespace std;
struct student
{
int id;
string name;
friend bool operator < (const student &A,const student &B)
{//指定排序
if(A.id<B.id)
return true;
if(A.id==B.id)
return A.name.compare(B.name)<0;
return false;
}
}s;//学生信息
int main()
{
map<student,int>mapp;
map<student,int>::iterator iter;
s.id=1;
s.name="student_one";
mapp.insert(pair<student,int>(s,90));
s.id=2;
s.name="student_two";
mapp.insert(pair<student,int>(s,80));
for(iter=mapp.begin();iter!=mapp.end();++iter)
{
cout<<iter->first.id<<" "<<iter->first.name<<" "<<iter->second<<endl;
}
return 0;
}
//当没有friend时候注意参数是右操作数
map类的定义类型
map<k,v>::key_type | 在map容器中,用做索引的类型 |
map<k,v>::mapped_type | 在map容器中,键所关联的值类型 |
map<k,v>::value_type | 一个pair类型,它的first元素具有const map<k,v>::key_type类型,而second元素则为map<k,v>::mapped_type类型 |
在学习map的借口时,需谨记valuetype是pair类型,它的值成员可以修改,但键成员不能修改
map迭代器进行解引用将产生pair类型的对象
二:给map添加元素
a.用insert函数插入pair数据
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
map<int,string>mapp;
mapp.insert(pair<int,string>(1,"chenchen"));
mapp.insert(pair<int,string>(2,"yuhongfu"));
mapp.insert(pair<int,string>(3,"hangzhou"));
map<int,string>::iterator iter;
for(iter=mapp.begin();iter!=mapp.end();++iter)
cout<<iter->first<<" "<<iter->second<<endl;
return 0;
}
b.用insert函数插入value_type数据
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
map<int,string>mapp;
mapp.insert(map<int,string>::value_type(1,"chenchen"));
mapp.insert(map<int,string>::value_type(2,"yuhongfu"));
mapp.insert(map<int,string>::value_type(3,"hangzhou"));
map<int,string>::iterator iter;
for(iter=mapp.begin();iter!=mapp.end();++iter)
cout<<iter->first<<" "<<iter->second<<endl;
return 0;
}
c.用数组方式插入数据
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
map<int,string>mapp;
mapp[1]="sdafsdf";
mapp[2]="fdsaf";
mapp[3]="effdhjfl";
map<int,string>::iterator iter;
for(iter=mapp.begin();iter!=mapp.end();++iter)
cout<<iter->first<<" "<<iter->second<<endl;
return 0;
}
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的 插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对 应的值 第三种如果然如键相同那么值就不同会改变
//验证插入函数的作用效果
#include <map>
#include <string>
#include <iostream>
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, "student_one"));
if(Insert_Pair.second == true)
cout<<"Insert Successfully"<<endl;
else
cout<<"Insert Failure"<<endl;
Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
if(Insert_Pair.second == true)
cout<<"Insert Successfully"<<endl;
else
cout<<"Insert Failure"<<endl;
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[1] = "student_one";
mapStudent[1] = "student_two";
mapStudent[2] = "student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
}
三:map的遍历
上述插入都含有遍历,就是迭代器
这里是数组形式的遍历
#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"));
mapStudent.insert(pair<int, string>(3, "student_three"));
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;
}
输出的结果是second
四:查找并读取map中的元素
s.count(k) | 返回s中k出现的次数 |
s.find(k) | 如果s容器中存在按k索引的元素,则返回指向该元素色迭代器,如果不存在,则返回超出末端迭代器 |
第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,
就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了,因为对于map对象,count成员的
返回值只能是0或1了,map容器只许一个键对应一个实例
#include<bits/stdc++.h>
#include<map>
#include<iostream>
using namespace std;
int main()
{
map<int,string>s;
s.insert(pair<int,string>(1,"chenchen"));
s.insert(pair<int,string>(2,"yuhongfu"));
s.insert(pair<int,string>(3,"hangzou"));
int len=s.size();
cout<<len<<endl;
map<int,string>::iterator iter;
cout<<s.count(1)<<endl;
return 0;
}
第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。
查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,
分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.
#include<bits/stdc++.h>
#include<map>
#include<iostream>
using namespace std;
int main()
{
map<int,string>s;
s.insert(pair<int,string>(1,"chenchen"));
s.insert(pair<int,string>(2,"yuhongfu"));
s.insert(pair<int,string>(3,"hangzou"));
int len=s.size();
cout<<len<<endl;
map<int,string>::iterator iter;
iter=s.find(1);
cout<<iter->first<<endl;
cout<<iter->second<<endl;
if(iter!=s.end())
cout<<"find"<<endl;
else
cout<<"no find"<<endl;
return 0;
}
第三种:
lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,而upper-bound(2)的话,返回的就是3
equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字,
#include<bits/stdc++.h>
#include<map>
#include<iostream>
using namespace std;
int main()
{
map<int,string>s;
s.insert(pair<int,string>(1,"chenchen"));
s.insert(pair<int,string>(2,"yuhongfu"));
s.insert(pair<int,string>(3,"hangzou"));
s.insert(pair<int,string>(5,"liufengyu"));
int len=s.size();
cout<<len<<endl;
map<int,string>::iterator iter;
iter=s.lower_bound(1);
cout<<iter->first<<" "<<iter->second<<endl;//:1 chenchen
iter=s.upper_bound(1);
cout<<iter->first<<" "<<iter->second<<endl;//:2 yuhongfu
pair<map<int,string>::iterator,map<int,string>::iterator>m;
m=s.equal_range(2);
if(m.first==m.second)
cout<<"no find"<<endl;
else
cout<<"find"<<endl;
m=s.equal_range(4);
if(m.first==m.second)
cout<<"no find"<<endl;
else
cout<<"find"<<endl;
return 0;
}
需要注意的是:
a 返回值是啥 count() find() lower_bound() upper_bound() equal_range()
b 查找的是关键字
五:从map对象中删除元素
从map容器中删除元素的erase操作有三种变化,与顺序容器一样,可向erase传递一个或一对迭代器,来删除单个元素或一段范围内的元素,其删除功能类似于顺序容器,但是有一点不同:map容器的erase操作返回void, 而顺序容器的erase操作则返回一个迭代器,指向被删除后面的元素
m.erase(k) | 删除m中键为k的元素,返回size_type类型的值,表示删除的元素个数 |
m.erase(p) | 从m中删除迭代器p所指向的元素。p必须指向m中确实存在的元素,而且不能等于m.end().返回void类型 |
m.erase(b,e) | 从m中删除一段范围内的元素,该范围由迭代器对b和e标记。b和e必须标记m中的一段有效范围:即b和e都必须指向m中的元 素或最后一个元素的下一个位置。而且,b和e要么相等(此时删除的范围为空),要么b所指向的元素必须出现在e所指向的元素之前 。返回void类型 |
#include<bits/stdc++.h>
#include<map>
#include<iostream>
using namespace std;
int main()
{
map<int,string>s;
s.insert(pair<int,string>(1,"chenchen"));
s.insert(pair<int,string>(2,"yuhongfu"));
s.insert(pair<int,string>(3,"hangzou"));
s.insert(pair<int,string>(5,"liufengyu"));
int len=s.size();
cout<<len<<endl;
map<int,string>::iterator iter;
//用迭代器删除
iter=s.find(1);
s.erase(iter);
map<int,string>::iterator it;
for(it=s.begin();it!=s.end();++it)
cout<<it->first<<" "<<it->second<<endl;
//把一这个删除了
//用关键字删除
int n=s.erase(2);//如果删除了就返回1,否则返回0
for(it=s.begin();it!=s.end();++it)
cout<<it->first<<" "<<it->second<<endl;
//把2删除了
s.erase(s.begin(),s.end());
//把整个map清空
//成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
for(it=s.begin();it!=s.end();++it)
cout<<it->first<<" "<<it->second<<endl;
return 0;
}
map的基本操作函数:
C++ maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数