zoukankan      html  css  js  c++  java
  • STL--map

    map--概述:

      映射(Map)和多重映射(Multimap)是基于某一类型Key的键集的存在,提供对TYPE类型的数据进行快速和高效的检索。
    l对Map而言,键只是指存储在容器中的某一成员。
    lMultimap允许重复键值,Map不允许。
    lMap和Multimap对象包涵了键和各个键有关的值,键和值的数据类型是不相同的,这与Set不同。
    Map内部数据的组织是一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在Map内部所有的数据Key都是有序的。
     
     

    map c

    产生一个空的map/multimap,其中不含任何元素

    map   c (op)

    以op为排序准则,产生一个空的map/multimap

    map   c1(c2)

    产生某个map/multimap的副本,所有元素均被复制

    map   c (beg, end)

    以区间[beg;   end]内的元素产生一个map/multimap

    map   c (beg, end, op)

    以op为排序准则,利用[beg;   end]内的元素生成一个map/multimap

    c.~map()

    销毁所有元素,释放内存

     
    元素的访问
    1.定义迭代器(iterator):map<string,float>::iterator pos;
    l其中map<string, float>表明这个迭代器的类型,声明一个迭代器pos,迭代器的角色类似于C/C++中的指针。
    2.当迭代器pos指向map容器中某个元素:
    l表达式pos->first获得该元素的key;
    l表达式pos->second获得该元素的value。
     
    题目:
    (会陆续添加)
     1.不能再裸啦! 再裸就不见一丝啦!
     1 #include<iostream>
     2 #include<map>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int n, ans, a, T;
     8     cin>>T;
     9     while(T--)
    10     {
    11         ans = 0;
    12         scanf("%d", &n);
    13         map<int,int> m;
    14         for(int i=0; i<n; i++)
    15         {
    16             scanf("%d",&a);
    17             m[a]++;
    18             if(m[a]>ans)
    19                 ans=m[a];
    20         }
    21         printf("%d
    ", ans);
    22         
    23     }
    24     return 0;
    25 }
    View Code

    3.裸裸裸裸,,,,,,,,,,切着玩吧!

    http://acm.hdu.edu.cn/showproblem.php?pid=1800

    #include<iostream>
    #include<cstdio>
    #include<map>
    using namespace std;
    
    int main()
    {
        int n;
        while(scanf("%d", &n)!=EOF)
        {
            int i, max =-1, q;
            map<int, int> M;
            for(i=0; i<n; i++)
            {
                scanf("%d", &q);
                M[q]++;
                if(max<M[q])
                max=M[q];
            } 
            printf("%d
    ", max);
        }
        return 0;
    }
    View Code

     2.我就是喜欢“裸体”。 来一发!

     
     1 #include <iostream>
     2 #include <map>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int main() 
     7 {
     8     //freopen( "in.txt", "r", stdin );
     9     //freopen( "out.txt", "w", stdout );
    10     int n;
    11     while (cin>>n && n) 
    12     {
    13         map <string, int> Balloon;
    14         string s;
    15         for (int i=0; i<n; i++)  
    16         {
    17             cin>>s;
    18             Balloon[s]++;
    19         }
    20         int iMax = 0;
    21         map <string, int>::iterator point, loc;
    22         for (point=Balloon.begin(); point!=Balloon.end(); point++)
    23             if (iMax<point->second) 
    24             {
    25                 iMax = point->second;
    26                 loc = point;
    27             }
    28         cout<<loc->first<<endl;
    29     }
    30     return 0;
    31 }
    View Code

     3.此题较难一点, 草滩小恪读懂题意都很费劲(英语渣的悲哀!哭,哭,哭,)。

    #include<iostream>
    #include<map>
    #include<set>
    using namespace std;
    
    typedef map<int, multiset<int> >line;
    map<int, multiset<int> >mx;
    map<int, multiset<int> >my;
    
    int bomb(line &x, line &y, int pos)
    {
        int ret = x[pos].size();
        multiset<int>::iterator it;
        for(it=x[pos].begin(); it!=x[pos].end(); it++)
        y[*it].erase(pos);
        x[pos].clear();
        return ret;
    } 
    
    int main()
    {
        int n, m, c, d, tx, ty;
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            if(n==0&&m==0) break;
            mx.clear();
            my.clear();
            for(int i=0; i<n; i++)
            {
                scanf("%d%d", &tx, &ty);
                mx[tx].insert(ty);
                my[ty].insert(tx);
            }
            for(int i=0; i<m; i++)
            {
                scanf("%d%d", &c, &d);
                int ans;
                if(c==0) ans = bomb(mx, my, d);
                else ans = bomb(my, mx, d);
                printf("%d
    ", ans);
            }
            printf("
    ");
        }
        return 0;
    }
    View Code

     4.这个题不太难, 就是需要些巧妙地读入处理技巧。

    http://acm.hdu.edu.cn/showproblem.php?pid=1075

    #include<cstdio>
    #include<iostream>
    #include<string>
    #include<map>
    using namespace std;
    
    map<string,string>mp;
    int main()
    {
       // freopen("in.txt","r",stdin);
      //  freopen("out.txt","w",stdout);
        mp.clear();
        string str1,str2;
        cin>>str1;
        while(cin>>str1)
        {
            if(str1=="END")break;
            cin>>str2;
            mp[str2]=str1;
        }
        cin>>str1;
        char ch;
        ch=getchar();
        str1="";
        while(1)
        {
            while(1)
            {
                scanf("%c",&ch);
                if(!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')))break;
                str1+=ch;
            }
            if(str1=="END")break;
            if(mp.find(str1)==mp.end())cout<<str1;
            else cout<<mp[str1];
            str1="";
            printf("%c",ch);
        }
        return 0;
    }
    View Code

    但是, 如果这道题的单词数据很大, 那么上面的代码就不行啦, 虽然map是基于红黑树的, 在处理重复单词时并没体现出特别的高效。 一个很好的方法就是--字典树!

  • 相关阅读:
    让xamarin的Entry绑定时,支持Nullable类型
    xamarin.forms 绑定页面里指定元素的某个属性值
    俄文环境下,字符串转数字失败
    使用devstack安装openstack
    在linux中访问macos 下的分区。
    git 多用户多仓库配置
    Macos上 ProxyChains 的坑, 需要关闭 sip
    MacOS 的预览 Preview 打开pdf 容易卡死 解决方案
    一客户端使用多个密钥对登录多个主机的解决方案.
    MacOS 10.13.6 下装xcode 流程
  • 原文地址:https://www.cnblogs.com/acm1314/p/4540599.html
Copyright © 2011-2022 走看看