关于map的一个习题,忽略大小写和标点符号单词的计数器

 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());
}

猜你喜欢

转载自blog.csdn.net/digitalkee/article/details/112962855
今日推荐