经典的使用关联数组的单词计数程序

来源:互联网 发布:佳能格式转换软件 编辑:程序博客网 时间:2024/05/29 04:38
使用关联容器,单词计数程序。运行时输入: dog dog cat回车,ctrl+z结束输入,回车运行。
#include <iostream>using namespace std;#include <map>#include <string>using std::string;int main(){map<string, size_t> wordCount;string word;while (cin >> word){++wordCount[word];}for (const auto &w : wordCount)cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << endl;system("pause");return 0;}
输入示例:
dog dog dog cat cat cat cat happy^Zcat occurs 4 timesdog occurs 3 timeshappy occurs 1 time请按任意键继续. . .
原创粉丝点击