用上篇文章的排序方法可以很方便的实现这个功能
代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
typedef pair<string, int> PAIR;
struct CmpByKeyLength {
bool operator()(const string& k1, const string& k2) {
if (strcmp(k1.c_str(), k2.c_str()) == 1)
return true;
else return false;
}
};
bool cmpVal(const PAIR& A, const PAIR& B)
{
return A.second>B.second;
}
void solution(string str)
{
map<string, int, CmpByKeyLength> res;
string temp;
int p = 0, q = 0;
for (int i = 0; i<str.size(); i++)
{
while ((str[i] >= 'A'&&str[i] <= 'Z') || (str[i] >= 'a'&&str[i] <= 'z'))
{
temp += str[i];
i++;
}
if (temp.size()>0)
{
res[temp]++;
temp = "";
}
}
cout &