#include using namespace std; //替换字符串 string &replace_all_distinct(string &str, const string &old_value, const string &new_value) { for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) { if ((pos = str.find(old_value, pos)) != string::npos) str.replace(pos, old_value.length(), new_value); else break; } return str; } //分割字符串 void split(const string &s, vector &sv, const char flag = ' ') { sv.clear(); istringstream iss(s); string temp; while (getline(iss, temp, flag)) { //sv.push_back(stoi(temp)); //如果需要返回整数 sv.push_back(temp); } return; } //unordered_map 按value进行排序 bool comp(const pair &a, const pair &b) { return a.second > b.second; } int main() { //输入+输出重定向 //freopen("../x.in", "r", stdin); //freopen("../x.out", "w", stdout); string s; getline(cin, s); string needReplaceArr[4] = {"?", ".", ",", "!"}; //去掉无用的标点符号 for (int i = 0; i < 4; ++i) { replace_all_distinct(s, needReplaceArr[i], ""); } //全部转小写! transform(s.begin(), s.end(), s.begin(), ::tolower); //按空格进行分割 vector v1; split(s, v1, ' '); //声明uno unordered_map _map; for (int i = 0; i < v1.size(); ++i) { if (v1[i] != "") _map[v1[i]]++; } //遍历_map,找到值最大的 vector > b; for (auto x:_map) { b.push_back(x); } //通过自定义排序方式进行排序 sort(b.begin(), b.end(), comp); //遍历的方法 //for(auto x:b) // cout<< x.first<<" " <