练习11.4 :扩展你的单词计数程序,忽略大小写和标点。
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, int> word;
string s;
string s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string s2 = ",.!?";
while (cin >> s)
{
auto sit1 = s.begin();
auto sit2 = s.end();
if (find(s1.cbegin(), s1.cend(), *sit1) != s1.cend())
{
char temp = *sit1;
temp += 32;
s.erase(s.begin());
s.insert(s.begin(), temp);
++word[s];
}
else if (find(s2.cbegin(), s2.cend(), *(sit2-1)) != s2.cend())
{
s.erase(s.end() - 1);
++word[s];
}
else
{
++word[s];
}
}
for (const auto& s : word)
{
cout << "这段文字中共包含单词:" << s.first << " " << s.second << "个" << endl;
}
return 0;
}