- map能够自动对key进行字典序排序
#include <stdio.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
string str;
while(getline(cin, str)){
string tmp;
map<string, int> mp;
int i = 0;
for(; i < str.size(); ++i){
if(str[i] == ' ' || str[i] == '.' || str[i] == ','){
if(tmp != ""){
mp[tmp]++;
}
tmp = "";
}else{
tmp += tolower(str[i]);
}
}
//auto it = mp.begin();
//while(it != mp.end()){
// cout << it->first << ":" << it->second << endl;
// ++it;
//}
for(const auto& it : mp){
cout << it.first << ":" << it.second << endl;
}
}
return 0;
}
来源:CSDN
作者:Mrzhailiming
链接:https://blog.csdn.net/qq_44905386/article/details/104667999