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