取map容器的前10大(根据每一个value排序)元素

本文介绍了如何从map中找出出现次数最多的10个元素,通过将map中的元素复制到vector,然后利用partial_sort进行排序。讨论了partial_sort的效率、选用vector而非list的原因以及为何不能直接用map的迭代器构造vector。
摘要由CSDN通过智能技术生成

 XX公司的笔试题:

  给定一个仅包含英文字母和空格的字符串,请实现一个函数找出该字符串中出现次数最多的10个字母(不区分大小写)。

      当时的解答是:

#include <iostream>
#include <algorithm>
#include <map>

#include <vector>
using namespace std;

bool compare_map(const pair<char, int> &p1, const pair<char,int> &p2)
{
     return p1.second < p2.second;
}
char* Find_top10Char(const char* str)
{
     if (!str)
         return NULL;
     const char *pStr = str;
     static char res[11] ;

     memset(res, 0, sizeof(res));
     map<char,int> tmp;
     while (*pStr != '/0')
     {
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值