1042

// PATn.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<utility>
#include<algorithm>         //含有sort方法!
#include<functional>        //含有greater方法!



using namespace std;



int main()
{
    string str;
    getline(cin, str);

    map<char, unsigned> count_char;//map<char,unsigned, greater<char>>map通过设置第是那个属性,可以控制按键值升降排序。默认为less<T>降序!

    for (auto r : str)
    {
        if (isalpha(r))
        {
            r = tolower(r);
            ++count_char[r];
        }
    }

    vector<pair<char, unsigned>>    tmp_count_char(count_char.begin(),count_char.end());//将map转为vector,再调用sort方法排序!stable_sort,当比较值相等时,保持原序列相对位置!
    stable_sort(tmp_count_char.begin(), tmp_count_char.end(), [&](pair<char, unsigned> i, pair<char, unsigned> j) {return i.second > j.second; });
    //stable_sort(tmp_count_char.begin(), tmp_count_char.end(), [&](pair<char, unsigned> i, pair<char, unsigned> j) {return i.first < j.first; });

    //sort只能应用于线性容器,如vector、list、deque!
    //sort(count_char.begin(), count_char.end(), [&](pair<char, unsigned> i, pair<char, unsigned> j) {return i.second > j.second; });
    //stable_sort(count_char.begin(), count_char.end());

    auto tmp = tmp_count_char.begin();
    cout << tmp->first << " " << tmp->second;


    system("pause");
    return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 光·色彩·生命 光 让色彩有了生命 让生命有了色彩 于是 生命和色彩 闪烁出光芒
    一了0820阅读 2,662评论 4 7
  • 《小王子》是译本最多的法国文学名著。 这是法国当代一部有名的童话——法国作家圣·埃克苏佩里“为大人...
    沪上剑客阅读 4,741评论 0 2
  • 老爹是做什么工作的? 老爹啊,是个地道的农民,一辆牛车,一壶老酒,农活一干就是一天。收成好的时候,老爹...
    唐家七妹阅读 3,392评论 0 4
  • 我习惯把事情往坏的方面想,所以很容易不快乐。 面试通过了,旁边的人都在为我高兴,只有我忧心着之后的笔试和未完结的论...
    From旮旯阅读 1,313评论 0 0