每次自己写map遍历总是出错。。自己也太水了。
然后网上借鉴了一下map如何用iterator遍历,修改了一下,于是一次过了。
#include <iostream>
#include <map>
#include <cctype>
using namespace std;
int main() {
string s;
getline(cin,s);
map <char,int> m;
for(int i=0; i<s.length(); i++) {
if(s[i]!=' ' && isalpha(s[i])) {
m[tolower(s[i])]++;
}
}
int Max=0;
char temp;
map<char, int>::iterator it;
it = m.begin();
while(it != m.end()) {
//cout << iter->first << " : " << iter->second << endl;
if(it->second>Max){
Max=it->second;
temp=it->first;
}
it++;
}
cout<<temp<<" "<<Max;
return 0;
}