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\n", 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\n", max);
    }
    return 0;
}
View Code

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2021-06-04
  • 2021-05-17
猜你喜欢
  • 2021-07-04
  • 2021-05-22
  • 2021-11-06
  • 2021-07-21
相关资源
相似解决方案