c++中的map

来源:互联网 发布:js获取回调函数返回值 编辑:程序博客网 时间:2024/06/01 18:54

c++提供的map容器,头文件#include<map>,map的特点是存储的都是键值对,键具有唯一性。键值对之间是一一对应的关系。如:map<string,int> color;那么键的类型是string,值是int.color[red]=1;

相当于一种特殊的数组,数据排序不用是int 。

Problem 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.
 

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.
 

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
5greenredblueredred3pinkorangepink0
 

Sample Output
redpink

#include<iostream>#include<stdio.h>#include<map>#include<string>using namespace std;int main(){int N;int max;string str;string index;map<string,int>color;while(scanf("%d",&N),N){max=0;for(int i=0;i<N;i++){cin>>str;color[str]++;if(color[str]>max){index=str;max=color[str];}}cout<<index<<endl;}return 0;}


原创粉丝点击