1、map:映射
map翻译为映射,也是常用的STL容器,map可以将任何基本类型(包括STL容器、string型)映射到任何基本类型(包括STL容器)。
2、map定义
map<typename1,typename2> mp //map内的<>类似键值对的形式,typename1为map映射前的类型(键key),typename2为map映射后的类型(值value),mp为变量名。
如果是字符串到整型的映射,必须使用string而不能用char数组:
map<string,int> mp;
2、map容器内元素的访问
map一般有两种访问方式:通过下标访问或通过迭代器访问。
(1)通过下标访问
使用和访问普通数组是一样的,例如对一个定义为map<char,int>mp的map来说,就可以直接mp['c']的方式来访问它对应的整数。注意:map中的键是唯一的。
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
map<char,int> mp;
mp['c']=20;
mp['c']=30; //20被覆盖
printf("%d ",mp['c']);//输出30
return 0;
}
(2)通过迭代器访问
map<typename1,typename2>::iterator it;
map的每一对映射有两个typename,这决定了必须能通过一个it来访问键和值,即it->first来访问键,it->second来访问值。
#include <stdio.h>
#include <map>
using namespace std;
int main()
{
map<char,int> mp;
mp['m']=20;
mp['r']=30;
mp['a']=40;
for(map<char,int>:: iterator it=mp.begin();it!=mp.end();it++)
{
printf("%c %d\n",it->first,it->second);
}
return 0;
}
3、map查找函数find()
find(key)返回键为key的映射的迭代器。
#include <stdio.h>
#include <map>
using namespace std;
int main()
{
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int>::iterator it=mp.find('b');
printf("%c,%d ",it->first,it->second);
return 0;
}
4、删除元素:erase()
(1)删除单个元素
1)迭代器删除:mp.erase(it) ,it为需要删除的元素的迭代器。
2)键删除:mp.erase(key),key为欲删除的映射的键。
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int> ::iterator it=mp.find('b');
//迭代器删除
mp.erase(it); //删除b 2
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
printf("%c %d\n" ,it->first ,it->second);
}
//键删除
mp.erase('c'); //删除c 2
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
printf("%c %d\n" ,it->first ,it->second);
}
return 0;
}
(2)删除一个区间
mp.erase(first,last),first为需要删除的区间的起始迭代器,而last为需要删除的区间末尾迭代器的下一个地址,也即删除左闭右开的区间[first,last)。
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int> ::iterator it=mp.find('b');
//迭代器删除
mp.erase(it,mp.end()); //删除b 2
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
printf("%c %d\n" ,it->first ,it->second);
}
return 0;
}
5、map大小:size()
size()用来获得map中的映射的对数。
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int> ::iterator it=mp.find('b');
printf("%d ",mp.size()); //2对映射
return 0;
}
6、清空:clear()
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
map<char,int> mp;
mp['a']=1;
mp['b']=2;
mp['c']=3;
map<char,int> ::iterator it=mp.find('b');
mp.clear(); //清空map
printf("%d ",mp.size()); //0对映射
return 0;
}
7、其他
(1)当访问map中不存在的键时会返回什么?
初步结论:map<typename1,typename2> mp; 无论typename2设为什么类型(int、char)返回结果应该为0,整数型。
1)首先验证与typename2无关:
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
map<char,int> mp;
//返回的结果还与打印输出类型有关,所以使用判断语句
if(mp['b']==0)
{
printf("%d\n",mp['b']);//输出0
}
map<char,char> mp1;
if(mp1['e']==0)
{
printf("%d\n",mp1['e']);//输出0,如果改为%c,输出结果为空格
}
return 0;
}
2)验证返回为0而不是空格
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
//特意选择typename2为char类型
map<char,char> mp1;
if(mp1['e']==0)
{
printf("111%c\n",mp1['e']);//输出空格,如果改为%d,输出结果为0
}
if(mp1['e']==' ')
{
printf("222%c\n",mp1['e']);//没有执行,返回的不是空格
}
return 0;
}
3)其他
打印空格解释(猜测):ASCII表中0对应空格,返回结果为0,但是如果以字符形式输出,会转换成ASCII表中的空格。