头文件
#include<map>
比较vector和map词典,vector的结构类似于:
而map的设立是为了也是解决通过下标访问元素,但此时下标和元素都可以是不同的数据类型,但此时下标更加专业的名称是键值(key),而元素的更加专业的名称是值(value)。一个键和元素的对应就是一个键值对(pair)。
map本质上是一个集合,容易对问题进行模拟出来。
常用的函数包括以下,能满足增删改遍历操作
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<map>
#include<unordered_map>
using namespace std;
int main(){
map<char,int> map1;//有序,但是不允许重复,省空间但是不省时间
//char是键的类型 int是值的类型。
multimap<char,int> map2;//有序但是允许重复,允许的是键重复,一个键可能会对应很多值
unordered_map<char,int> map3;//无序但是不允许重复,经常使用,因为时间开销小
unordered_multimap<char,int> map4;
//键值对
pair<char,int> pair1 = {'w',0};//map里基本的元素
//first 提取键 second 提取值
printf("key = %c,value = %d\n",pair1.first,pair1.second);//输出key = w,value = 0
//map中新增
map1.insert(pair1);
map2.insert(pair1);
//map2.insert({'w',2});这里是可以的,因为允许键值重复
map2.insert({'o',1});
//map中删除
map1.erase('w');//只需erase键的值就可以。
map2.erase('w');
//遍历map
map<char,int> mapi ={
{'w',0},{'o',1},{'r',2},{'l',3},{'d',4}
};
map<char,int>::iterator it;
for(it = mapi.begin();it != mapi.end();it++){
printf("key = %c,value = %d \n",it->first,it->second);
}
//便捷访问
printf("value = %d\n",map1['o'])//这里的'o'是指键,输出的是值
printf("value = %d\n",map1['a'])//如果'a'这个键不存在的话,那就会新增一个值,默认值是0;
//给键赋值
map['o'] = 1;//类似数组里面的a[i] = 1;
//如果想要有才输出,没有就不输出的话,对应的是map,multimap不可以,因为multimap不支持[],因为一个键对应很多值
if(map1.find('o') == map1.end()){//这里意思是o这个键值不存在于map1中
printf("key is not in map\n");
}
else
printf("value = %d\n",map1['o']);
//对multimap来说的访问,有才输出,没有不输出,因为multimap不支持[]运算符
//lower_bound(key)返回key对应的第一个值的位置
//upper_bound(key)返回的是key对应的最后一个值的后一个位置
for(it = map1.lower_bound('o');it != map1.upper_bound('o');it++){//这里是遍历所有键相同的值,++只会找到所有的'o'键
printf("key = %c,value = %d\n",it->first,it->second);
}
return 0;
}
看题目:
请你计算按照手机键盘(99 键输入法)输入字母的方式,键入给定字符串(由小写字母构成)所花费的时间。
具体键入规则和花费时间如下描述:
对于同一键上的字符,例如 a,b,c 都在 “1” 键上,输入 a只需要按一次,输入 c 需要连续按三次。
如果连续两个字符不在同一个按键上,则可直接按,如:ad 需要按两下,kz 需要按 66 下。
如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如 ac,在按了 a 之后,需要等一会儿才能按 c。
现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。
输入格式
输入包含多组测试数据。
每组数据占一行,包含一个由小写字母构成的字符串。
输出格式
对于每组输入,输出一行结果表示键入给定字符串所需要花费的时间。
数据范围
每个输入最多包含 100 组测试数据。
所有字符串的长度都不超过 100。
输入样例:
bob
www
输出样例:
7
7
先贴代码:
#include<iostream>
#include<unordered_map>
#include<map>
#include<string>
#include<cstdio>
#include<stdio.h>
using namespace std;
int main(){
multimap<char,int> map1 = {{'a',1},{'b',2},{'c',3},{'d',1},{'e',2},{'f',3},{'g',1},{'h',2},{'i',3},{'j',1},{'k',2},{'l',3},{'m',1},{'n',2},{'o',3},{'p',1},{'q',2},{'r',3},{'s',4},{'t',1},{'u',2},{'v',3},{'w',1},{'x',2},{'y',3},{'z',4}};
multimap<char,int> map2 = {
{'a',2},{'b',2},{'c',2},
{'d',3},{'e',3},{'f',3},
{'g',4},{'h',4},{'i',4},
{'j',5},{'k',5},{'l',5},
{'m',6},{'n',6},{'o',6},
{'p',7},{'q',7},{'r',7},{'s',7},
{'t',8},{'u',8},{'v',8},
{'w',9},{'x',9},{'y',9},{'z',9},
};
string str;
int time = 0;
multimap<char,int>::iterator it;
while(cin>>str){
time = 0;
for(int i = 0;i < str.size(); i++){
auto a = map2.find(str[i]);
auto b = map2.find(str[i-1]);
if(map2.find(str[i])->second == map2.find(str[i-1])->second)time+=2;
for(it = map1.begin();it != map1.end();it++){
if(str[i] == it->first)time += it->second;
}
}
cout<<time<<endl;
}
}
注意find函数的返回值是一个地址,而非second值,所以应当iterator一个迭代器,指向的是find函数返回的地址,最后用second去比较是否处在同一个按键中。