1042
2017-09-03 本文已影响0人
峡迩
// 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;
}