zoukankan      html  css  js  c++  java
  • STL之map篇

      

         度熊所居住的 D 国,是一个完全尊重人权的国度。以至于这个国家的所有人命名自己的名字都非常奇怪。一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字。例如,如果一个人名字是 ACM,那么 AMC, CAM, MAC, MCA, 等也都是这个人的名字。在这个国家中,没有两个名字相同的人。

    度熊想统计这个国家的人口数量,请帮助度熊设计一个程序,用来统计每一个人在之前被统计过多少次。

    Input

    这里包括一组测试数据,第一行包含一个正整数N,接下来的N 行代表了 N个名字。N 不会超过100,000,他们的名字不会超过40位.

    Output

    对于每输入的一个人名,输出一个整数,代表这个人之前被统计了多少次。

    Sample Input
    5
    ACM
    MAC
    BBA
    ACM
    BAB
    
    Sample Output
    0
    1
    0
    2
    1

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<string>
    #include<iterator>
    using namespace std;
    bool compare(char c1,char c2)
    {
        return c1 <= c2;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            map<string,int> msi;
            for(int i=0;i<n;++i)
            {
                char temp[45];
                scanf("%s",temp);
                sort(temp,temp+strlen(temp),compare);
                string str(temp);
                //使用find来判断map中是否含有该key
                map<string,int>::iterator it = msi.find(str);
                if(it==msi.end())
                {
                    //使用pair来判断是否插入成功
                    pair<map<string,int>::iterator,bool> res = msi.insert(pair<string,int>(str,0));
                    if(res.second==true)
                    {
                        cout<<"OK"<<endl;
                    }
                    else
                    {
                        cout<<"FALSE"<<endl;
                    }
                }
                else
                {
                    msi[str]++;
                }
                printf("%d
    ",msi[str]);
            }
    
        }
        return 0;
    }
    View Code

       1.如何判断map插入成功或者失败:pair<map<string,int>::iterator,bool> res = msi.insert(pair<string,int>(key,value)); res.second的值

       2.如何判断map中是否包含某key:map<string,int>::iterator it = msi.find(key);   判断it == msi.end();

       3.msi[key] = value;如果key没有被insert过,会通过该式子进行insert。

       hdu1004

      

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string.h>
    #include<map>
    #include<iterator>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
        int n;
        while(~scanf("%d",&n),n)
        {
            map<string,int> msi;
            for(int i=0;i<n;++i)
            {
                char cstr[20];
                scanf("%s",cstr);
                string str(cstr);
                map<string,int>::iterator it = msi.find(str);
                if(it != msi.end())
                {
                    ++msi[str];
                }
                else
                {
                    msi[str] = 1;
                }
            }
            string strResult;
            int maxnum = 0;
            map<string,int>::iterator it;
            for(it = msi.begin();it != msi.end();it++)
            {
                if(it->second > maxnum)
                {
                   maxnum = it -> second;
                   strResult = it -> first;
                }
            }
            printf("%s
    ",strResult.c_str());
        }
        return 0;
    }
    View Code

       1、map的insert是默认以key排序的

       2、map没有sort方法。

  • 相关阅读:
    ios 截图图片
    更改AlertView背景
    如何卸载编译安装的源码包(mysql卸载)
    测试6
    curl 测试websocket请求 whitesky
    JVM中的垃圾收集
    Java面试题
    Java的四种引用
    一款吊炸天的AI图片增强工具!
    LiteFlow 2.6.4版本发行注记,里程碑版本!
  • 原文地址:https://www.cnblogs.com/jlyg/p/7197180.html
Copyright © 2011-2022 走看看