map水题目

先做一个水题目。为了熟悉map的基本用法。记得在沈阳实训的时候,老师介绍过stl,里头的关联容器之一就是map,其实是是一个绑定索引,我觉得,蛮好用,除了new什么之类的不懂,其余删除,加入基本都会。

#include <iostream>
#include<map>
#include<string>
using namespace std;

map<string,int>ballon;
string a;
string maxs;
int t,maxi;
int main()
{
   while(cin >> t){
       ballon.clear();
       maxi = 0;
       if(!t)break;
       for(int i = 0;i < t;i++){
       cin >> a;
       ballon[a]++;
       }

       map<string,int>::iterator p;
       for(p = ballon.begin();p!=ballon.end();p++)
       {

           if(p->second > maxi)
           {

               maxi = p->second;
               maxs = p->first;
           }
       }
       cout << maxs << endl;
   }
   return 0;
}
原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3880674.html