一.map 的定义
1.用途:可以将任何基本类型(包括STL容器)映射到任何基本类型;
2.map 头文件;
3.命名空间;
4.map 的定义:map<typename1, typename2> mp;
<键—,—>值>,数组不能做键值;
二.元素的访问
1.通过下标访问:和访问普通数组一样(map 中的键是唯一的),示例:
map<char, int> mp;
mp['c'] = 20;
mp['c'] = 30;
printf("%d\n", mp['c']);//输出30
2.通过迭代器访问:map<typename1, typename2>::iterator it;
通过 it->first 访问键,it->second 访问值,示例:
map<char, int> mp;
mp['m'] = 20;
mp['a'] = 30;
map<char, int>::iterator it = mp.begin();
printf("%c %d\n", it->first, it->second);
//输出:a 30
map 会以键从小到大的顺序自动排序(因为 map 内部是使用红黑树实现的);
三.常用函数
1.find():find(key) 返回键为 key 的映射的迭代器,示例:
map<char, int> mp;
mp['a'] = 1;
mp['b'] = 2;
mp['c'] = 3;
map<char, int>::iterator it = mp.find('b');
printf("%c %d\n", it->first, it->second);
//输出:b 2
2.erase():
1).删除单个元素:
a. mp.erase(it),it 是迭代器;
b. mp.erase(key),key 是键;
2).删除区间内元素:mp.erase(first, last),均为迭代器;
3.size();
4.clear();
四.常见用途
1.建立字符/字符串与整数/字符串之间的映射;
2.判断大整数或其他数据类型是否存在,可以把 map 当作 bool 数组;
五.题目
1.PAT B1044
思路:
打表(0~168);
注意:
1).scanf("%d%*c", &n);
2).getline(cin, str);
与cin<<str;
有什么区别?
3).数字字符转数字:字符-‘0’;
代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
using namespace std;
string unitDigit[13] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string tenDigit[13] = {"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
map<string, int> strToNum;
string numToStr[170];
void init() {
for(int i = 0; i <= 12; i++) {
numToStr[i] = unitDigit[i];
strToNum[unitDigit[i]] = i;
numToStr[i * 13] = tenDigit[i];
strToNum[tenDigit[i]] = i * 13;
}
for(int i = 1; i < 13; i++) {
for(int j = 1; j < 13; j++) {
numToStr[i * 13 + j] = tenDigit[i] + " " + unitDigit[j];
string temp = tenDigit[i] + " " + unitDigit[j];
strToNum[temp] = i * 13 + j;
}
}
}
int main() {
int n;
scanf("%d%*c", &n);
init();
while(n--) {
string str;
int num = 0;
getline(cin, str);
if(str[0] >= '0' && str[0] <= '9') {
int len = str.size();
for(int i = 0; i < len; i++) {
num += (str[len - 1 - i] - '0') * pow(10, i);
}
cout<<numToStr[num]<<endl;
}
else cout<<strToNum[str]<<endl;
}
return 0;
}
2.PAT A1054
思路:
用 map<int, int> ,因为数据大,用数组可能内存超限;
代码:
#include<cstdio>
#include<map>
using namespace std;
map<int, int> count;
int main() {
int c, r;
scanf("%d%d", &c, &r);
long int num;
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
scanf("%d", &num);
count[num]++;
}
}
int sum = 0;
int ans;
for(map<int, int>::iterator it = count.begin(); it != count.end(); it++) {
if(it->second > sum) {
sum = it->second;
ans = it->first;
}
}
printf("%d", ans);
return 0;
}
3.PAT A1071
思路:
1.书上思路:
while(i < input长度) {
while(是单词)i++
if(单词非空)map
while(不是单词) i++
}
2.我的思路:类似于状态机,多单独判断一个首尾;
注意:
怎么写 mp.[str]++:如果之前有 str 则 +1;若没有则等于1;
代码:(我的思路,测试点4通不过,why?)
#include<cstdio>
#include<map>
#include<string>
#include<iostream>
using namespace std;
map<string, int> mp;
int main() {
string input;
getline(cin, input);
int m = 0;
string str;
if(input[0] >= 'A' && input[0] <= 'Z') input[0] += 32;
if((input[0] >= 'a' && input[0] <= 'z') || (input[0] >= '0' && input[0] <= '9'))
str += input[0];
for(int i = 1; i < input.size(); i++) {
if(input[i] >= 'A' && input[i] <= 'Z') input[i] += 32;
if((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= '0' && input[i] <= '9')) {
str += input[i];
if(i == input.size() - 1) {
if(mp.find(str) == mp.end()) mp[str] = 1;
else mp[str]++;
str.clear();
}
}
else {
if((input[i - 1] >= 'a' && input[i - 1] <= 'z') || (input[i - 1] >= '0' && input[i - 1] <= '9')) {
if(mp.find(str) == mp.end()) mp[str] = 1;
else mp[str]++;
str.clear();
}
}
}
int sum = 0;
string ans;
for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
if(it->second > sum) {
sum = it->second;
ans = it->first;
}
}
cout<<ans<<" "<<sum<<endl;
return 0;
}