map:映射
#include <map>
map是从键得到值的映射,由于重载了[ ]运算符,所以可以像数组取下标一样去使用它。
在map中,数据是默认以键的从小到大来排序的(类似于集合)。
如果要定义一个map,可以这样定义:map<string, int> a;
注意string类的地方可以用字符串的字符数组来代替
map的一些操作:
插入元素
#include <map>
map<string, int> a;
插入元素方法1:
a["str1"]=1;
a["str2"]=2;
插入元素方法2:
a.insert(pair<string, int>("str3", 3));
插入元素方法3:
a.insert(map<string, int>::value_type("str4", 4));
查找元素
与迭代器一起用
当所查找的关键key出现时,它返回数据所在对象的位置,如果沒有,返回iter与end函数的值相同。
// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
iter = a.find("str2");
if(iter != a.end())
cout<<"Find"<<endl;
else
cout<<"Do not Find"<<endl;
刪除与清空元素
关键字刪除
a.erase("str1");
清空操作
a.clear();
迭代器刪除
iter = a.find("str1");
a.erase(iter);
用迭代器范围刪除 : 把整个map清空
a.erase(a.begin(), a.end());
其他操作
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
erase() 删除一个元素
find() 查找一个元素
insert() 插入元素
lower_bound() 返回键值>=给定元素的第一个位置
size() 返回map中元素的个数
swap() 交换两个map
应用:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
int main()
{
map<int, string> a;
a[1]="abc";
a[2]="def";
a[3]="ghi";
map<int, string>::iterator iter=a.begin();
for (; iter!=a.end(); iter++)
cout<<iter->first<<" "<<iter->second<<endl;
//->first是iter的key ->second是iter的value
return 0;
}
CodeForces 2A Winner
The winner of the card game popular in Berland “Berlogging” is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line “name score”, where name is a player’s name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It’s guaranteed that at the end of the game at least one player has a positive number of points.
Input
The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in “name score” format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
Output
Print the name of the winner.
Examples
Input
3
mike 3
andrew 5
mike 2
Output
andrew
Input
3
andrew 3
andrew 2
mike 5
Output
andrew
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
map<string, int> a, b;
int main()
{
string str[1005];
int score[1005];
int n;
scanf("%d", &n);
for (int i=1; i<=n; i++){
cin>>str[i];
scanf("%d", &score[i]);
a[str[i]]+=score[i];
}
int ans=0;
map<string, int>::iterator iter=a.begin();
for (; iter!=a.end(); iter++){
if (iter->second>ans)
ans=iter->second;
}
for (int i=1; i<=n; i++){
b[str[i]]+=score[i];
if (b[str[i]]>=ans && a[str[i]]>=ans){
cout<<str[i];
break;
}
}
return 0;
}