S - stl 的mapⅠ

 

先来介绍一下stl中的map这个功能

头文件#include<map>

mapSTL的一个关联容器,它提供一对一的数据处理能力

就像一个人对应一个编号一样

定义 为  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>(101, "People_1"));
       mapStudent.insert(pair<int, string>(102, "People_2"));
       mapStudent.insert(pair<int, string>(103, "People_3"));
       map<int, string>::iterator  iter;
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
       cout<<iter->first<<"  " <<iter->second<<endl;
}
     //  system("pause");
    return 0;
}

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()

{
       map<int, string> mapPeople;
       mapStudent.insert(map<int, string>::value_type (101, "People_1"));
       mapStudent.insert(map<int, string>::value_type (102, "People_2"));
       mapStudent.insert(map<int, string>::value_type (102, "People_3"));
       map<int, string>::iterator  iter;
      for(iter = mapPeople.begin(); iter != mapPeople.end(); iter++){
       cout<<iter->first<<" "<<iter->second<<endl;
}
      // system("pause");
    return 0;
}

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       map<int, string> mapPeople;
       mapPeople[101] =  "People_1";
       mapPeople[102] =  "People_2";
       mapPeople[103] =  "People_3";
       map<int, string>::iterator  iter;
       for(iter = mapPeople.begin(); iter != mapPeople.end(); iter++){
       cout<<iter->first<<" "<<iter->second<<endl;
}
     //  system("pause");
    return 0;
}

利用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.        
                

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

5
green
red
blue
red
red
3
pink
orange
pink
0
                

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=0;
        string m="\0";
        ballon.clear();
        for(int i=0;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 0;
}

 

 
 
posted @ 2016-02-23 21:50  Not-Bad  阅读(186)  评论(0编辑  收藏  举报