使用双指针截取字符串
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main(){
string s;
while(getline(cin,s)){
map<string,int> counter;
string::iterator pre = s.begin();
string::iterator end = s.begin();
while(pre!=s.end()){
while(*end!=' '&&*end!='.') end++;
string word = s.substr(pre-s.begin(),end-pre);
transform(word.begin(),word.end(),word.begin(),::tolower);
if(counter.find(word)!=counter.end()){
counter[word]++;
} else {
counter[word] = 1;
}
if(*end=='.'){
break;
} else {
end++;
pre = end;
}
}
map<string,int>::iterator it = counter.begin();
while(it!=counter.end()){
cout<<it->first<<":"<<it->second<<endl;
it++;
}
}
return 0;
}

京公网安备 11010502036488号