hdu1004

1 篇文章 0 订阅

Map 简介:

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key

Map的功能

1.自动建立Key value的对应。key value可以是任意你需要的类型。

2.根据key值快速查找记录,查找的复杂度基本是    Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。

3.快速插入Key - Value 记录。

4.快速删除记录

5.根据Key 修改value记录。

6.遍历所有记录。

Map 迭代器:  //常用于遍历操作

map<int,string>::iteratoriter;

for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

  cout<<iter->first<<””<<iter->second<<end;

}

下面是该题代码

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
    map<string,int> cat;
    string str;
    int N;
    while(cin >> N && N)
    {
        cat.clear();
        for(int i = 0;i < N;i ++)
        {
            cin >> str;
            cat[str] ++;
        }
        map<string,int>::iterator ite;
        int maxnumber = 0; string maxcolor;
        for(ite = cat.begin();ite != cat.end();ite ++)
            if(ite -> second > maxnumber){
                maxnumber = ite -> second;
                maxcolor = ite -> first;
            }
        cout << maxcolor << endl;
    }
    return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值