#include <iostream>
using namespace std;
#include<map>
#include<algorithm>
#include<string>
#include<vector>
struct Compare
{
bool operator()(const pair<string,int>&a,const pair<string,int>&b)
{
return a.second>b.second;//比较的仿函数
}
};
int main()
{
string s;
map<string,int>Map;
getline(cin,s);//因为有空格,用geiline接收
char*ch;
for(int i=0;i<s.size();++i)
{
string ss;
while(s[i]!=' ')
{
if(s[i]=='.')
{
break;//处理句子末
}
ss+=tolower(s[i++]);//记得转换为小写
}
Map[ss]++;
}
vector<pair<string,int>>arr(Map.begin(),Map.end());
stable_sort(arr.begin(),arr.end(),Compare());//map容器是帮我们用字典序排好了的,只是不根据数量大小排序,stable_sort稳定的排序,不会影响arr中数量相同的元素的相对位置
for(auto it:arr)
{
cout<<it.first<<":"<<it.second<<endl;
}
return 0;
}
我们思路是先提取出单词,然后根据map与快排进行最后的顺序排列



京公网安备 11010502036488号