求出最喜欢的前k种最喜欢的水果

近几年cvte的面试题中有这样一个问题:
本公司现在要给公司员工发波福利,在员工工作时间会提供大量的水果供员工补充营养。由于水果种类比较多,但是却又不知道哪种水果比较受欢迎,然后公司就让每个员工报告了自己最爱吃的k种水果,并且告知已经将所有员工喜欢吃的水果存储于一个数组中。然后让我们统计出所有水果出现的次数,并且求出大家最喜欢吃的前k种水果。
1.题目分析:
(1)它要求统计出所有水果出现的次数,看到这个如果对STL容器比较熟悉的话肯定就会想起map,mutilmap或unordered_map,那么在这些容器中我们选择哪个比较合适呢?map和unordered_map底部的实现是不同的,前者是红黑树实现的,后者是哈希表实现的,但是unordered_map在Linux环境下是不存在的,选择使用哪种容器的时候需要注意平台环境。本次我用的是map实现的,其他的容器当然也是可行的。
(2)不仅如此,还需要我们求出大家最喜欢的前k种水果。一提起前k种相信大家一定会联想到topK问题的,也就是建堆问题,如果在线OJ编程,你去建个堆可能时间上就是不允许的,所以啊,还是应该想到我们的容器,其实priority_queue优先级队列就是建堆来实现的。

2.下面我们看看代码如何实现的:

struct Less
{
    bool operator()(map<string,int>::iterator it1,map<string,int>::iterator it2)
    {
        return it1->second < it2->second;
    }
};
void GetFavoriateFruits(const vector<string>& fruits, size_t k)
{
    map<string, int> countMap;
    map<string, int>::iterator countMapIt;
    //方法一:
    //for (size_t i = 0; i < fruits.size(); ++i)
    //{
    //  countMapIt=countMap.find(fruits[i]);
    //  if (countMapIt != countMap.end())
    //  {
    //      countMapIt->second++;    //该水果已经存在则只需要次数++
    //  }
    //  countMap.insert(make_pair(fruits[i],1));
    //}
    //方法二:
    //pair<map<string, int>::iterator, bool> ret;
    //for (size_t i = 0; i < fruits.size(); ++i)
    //{
    //  ret = countMap.insert(make_pair(fruits[i],1));
    //  if (ret.second == false)    //插入失败说明该水果已经存在
    //  {
    //      ret.first->second++;   //ret.first是map<string, int>::iterator
    //  }
    //}
    //方法三:利用operator[]来实现,内部其实也调用的是insert
    for (size_t i = 0; i < fruits.size(); ++i)
    {
        countMap[fruits[i]]++;
    }
    //打印出各种水果以及它们所对应出现的次数
    countMapIt = countMap.begin();
    while (countMapIt != countMap.end())
    {
        cout << countMapIt->first << " " << countMapIt->second << endl;
        ++countMapIt;
    }
    //前k种水果,优先级队列的内部实现就是通过建堆(用vector里的元素)
    priority_queue<map<string, int>::iterator, vector<map<string, int>::iterator>, Less> q;
    countMapIt = countMap.begin();
    while (countMapIt != countMap.end())
    {
        q.push(countMapIt);
        ++countMapIt;
    }
    for (size_t i = 0; i < k; ++i)
    {
        cout << "哪种水果:" << q.top()->first << " " << "出现的次数:" << q.top()->second << endl;
        q.pop();
    }
}

3.这里针对以上代码说明几个问题
(1)make_pair其实就是对pair结构体的一层封装
这里写图片描述
(2)map中的operator[ ],它的底层是如何实现的,为什么会达到值不存在时会成功插入,值存在时它的计数会增加?
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值