// PATn.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include #include #include #include #include //含有sort方法! #include //含有greater方法! using namespace std; int main() { string str; getline(cin, str); map count_char;//map>map通过设置第是那个属性,可以控制按键值升降排序。默认为less降序! for (auto r : str) { if (isalpha(r)) { r = tolower(r); ++count_char[r]; } } vector> 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 i, pair j) {return i.second > j.second; }); //stable_sort(tmp_count_char.begin(), tmp_count_char.end(), [&](pair i, pair j) {return i.first < j.first; }); //sort只能应用于线性容器,如vector、list、deque! //sort(count_char.begin(), count_char.end(), [&](pair i, pair 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; }