c++ primer5th,map的一个习题 11.4
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <cctype>
using namespace std;
void to_lower(string &s);
int main()
{
map<string,unsigned> words_count;
string str;
while(cin >> str)
{
to_lower(str);
words_count[str] ++;
}
for(const auto & i : words_count)
cout << i.first << " occurs " << i.second << (i.second > 1?"time":"times") << endl;
return 0;
}
void to_lower(string &s)
{
for(string::iterator it = s.begin();it != s.end();++ it)
{
*it = tolower(*it);
}
char ch = *(--s.end());
if(ch == ',' || ch == '.' || ch == '!')
s.erase(--s.end());
}