zoukankan      html  css  js  c++  java
  • S

     

    先来介绍一下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="";
            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;
    }
     
     
  • 相关阅读:
    html的输出&,空格,大小于号
    html如何修改hr水平直线的粗细
    LODOP指定window默认打印机和临时默认打印机
    微软面试题: 找出二叉树上任意两个结点的最近共同父结点。
    说说自己对hibernate一级、二级、查询、缓存的理解。
    MySql中添加用户,新建数据库,用户授权,删除用户,修改密码
    修改MySQL的默认密码的四种小方法
    java中Scanner的nextLine()和next()的区别
    JAVA中String字符串比较equals()和equalsIgnoreCase()的区别
    HashMap与HashTable的区别
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5211343.html
Copyright © 2011-2022 走看看