- /*=============================================================================
- # FileName: 4_word_count_ign.cc
- # Desc: 统计单词数量,忽略大小写和标点
- # HomePage:
- # Version: 0.0.1
- # History:
- =============================================================================*/
- #include <iostream>
- #include <map>
- #include <string>
- #include <cctype>
- #include <algorithm>
- using namespace std;
- int main(int argc, char *argv[])
- {
- string alp = "abcdefghigklmnopqrstuvwxyz";
- map<string, int> word_cnt;
- string word;
- while (cin >> word) {
- //转换成小写
- transform(word.begin(), word.end(), word.begin(),
- [](int c) ->int { return tolower(c); });
- word = word.substr(0,word.find_first_not_of(alp));
- ++word_cnt[word];
- }
- for (const auto &w : word_cnt) {
- cout << w.first << " occurs "
- << w.second << ((w.second > 1) ? " times" : " time")
- << endl;
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1808201410214.html
来源: http://www.codesnippet.cn/detail/1808201410214.html