先来介绍一下stl中的map这个功能
头文件#include<map>
map是STL的一个关联容器,它提供一对一的数据处理能力
就像一个人对应一个编号一样
定义 为 map<int, string> mapPeople;
insert 插入数据,如下有三种插入数据的方法
①
#include <map> #include <string>
#include <iostream>
using namespace std;
int main() { map<int, string> mapPeople;
mapStudent.insert(pair<int, string>(, "People_1"));
mapStudent.insert(pair<int, string>(, "People_2"));
mapStudent.insert(pair<int, string>(, "People_3"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
cout<<iter->first<<" " <<iter->second<<endl;
}
// system("pause");
return ;
}
②
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main() {
map<int, string> mapPeople;
mapStudent.insert(map<int, string>::value_type (, "People_1"));
mapStudent.insert(map<int, string>::value_type (, "People_2"));
mapStudent.insert(map<int, string>::value_type (, "People_3"));
map<int, string>::iterator iter;
for(iter = mapPeople.begin(); iter != mapPeople.end(); iter++){
cout<<iter->first<<" "<<iter->second<<endl;
}
// system("pause");
return ;
}
③
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapPeople;
mapPeople[] = "People_1";
mapPeople[] = "People_2";
mapPeople[] = "People_3";
map<int, string>::iterator iter;
for(iter = mapPeople.begin(); iter != mapPeople.end(); iter++){
cout<<iter->first<<" "<<iter->second<<endl;
}
// system("pause");
return ;
}
利用size这个函数得知map的大小int n = mapPeople.size();
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
删除map中的指定元素:
mapTest.erase(keyValue); //删除key==keyValue的所有元素 pos = mapTest.find(keyValue); //移除第一个key==keyValue的元素
if (pos != mapTest.end())
{
mapTest.erase(pos);
}
Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
This year, they decide to leave this lovely job to you.
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.
A test case with N = 0 terminates the input and this test case is not to be processed.
A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
Sample Output
red
pink
#include<iostream>
#include<string>
#include <map>
using namespace std;
int main() {
int n;
string color;
map<string,int>ballon;
while(cin>>n&&n){
int p=;
string m="\0";
ballon.clear();
for(int i=;i<n;i++){
cin>>color;
ballon[color]++;
}
map<string,int>::iterator t;
for(t=ballon.begin();t!=ballon.end();t++){
if((*t).second>p){
p=(*t).second;
m=(*t).first;
}
}
cout<<m<<endl;
}
//system("pause");
return ;
}