map是c++的一个关联容器 提供一对一的映射
第一个称为关键字key(有唯一性) 第二个称为关键字的值value
和python里的字典很像

[map] [c++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
如你想建立一个姓名-电话对应关系
定义一个map对象
map<string,string>friends;
cout<<friends.find("jack"); 找到jack的对应号码
cout<<friedns.find(123); 找123对应的姓名 找不到返回null

用insert函数插入
friends.insert(pair<string, string>("judy", "789"));

遍历 不能写小于
for(iter=friends.begin();iter!=friends.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}

推荐使用数组插入 因为对于同一关键字 新的值可以覆盖旧的
friends[234]="sam";
friends[456]="mark";

查找元素与删除 清空
iter=firends.find("123");
friends.erase(iter);
friends.clear;

查看有多少元素
int num=friends.size();

返回头部/尾部迭代器
friends.begin();
friends.end();

返回键值>=给定元素的第一个位置
lower_bound()

返回键值>给定元素的第一个位置
upper_bound();

整道简单例题

一看就用映射关系解决 注意要求谁先到最高分 所以多定义一个b

[map例] [c++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<bits/stdc++.h>
using namespace std;
int x[10010];
string s[10010]
map<string,int>a,b;//定义
int main()
{
int n;
cin>>n
for(int i=0;i<n;i++>)
{
cin>>s[i]>>x[i];
a[s[i]]+=x[i]; //赋value
}
int max=0; //通过a先求出最大值
for(int i=0;i<n;i++)
{
if(a[s[i]]>max) max=a[s[i]];
}
for(int i=0;i<n;i++)//再通过b求谁先到最大值
{
b[s[i]]+=x[i];
if((b[s[i]])>=max&&a[s[i]]>=max)
{
cout<<s[i];
break;
}
}
return 0;
}