练习11.4:扩展你的程序,忽略大小写和标点。例如,“example.”,“example,”和“Example”应该计数递增相同的计数器。
/*
*C++Primer(第五版)
*练习11.3
*2015/9/22
*问题描述:练习11.4:扩展你的程序,忽略大小写和标点。例如,"example.","example,"和"Example"应该计数递增相同的计数器。
附:练习11.3:编写你自己的单词记数程序
*说明:照着P375页copy了一遍
*作者:Nick Feng
*邮箱;nickgreen23@163.com
*/
#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
int main()
{
map<string, size_t> word_count;
set<string> exclude = {"example.","example,","Example"};//要被忽略计数的单词
string word;
while(cin >> word)
if(exclude.find(word) == exclude.end()) //修改部分
++word_count[word];
for(const auto &w : word_count)
cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << endl;
return 0;
}