练习11.3:编写你自己的单词记数程序
/*
*C++Primer(第五版)
*练习11.3
*2015/9/22
*问题描述:练习11.3:编写你自己的单词记数程序
*说明:照着P375页copy了一遍
*作者:Nick Feng
*邮箱;nickgreen23@163.com
*/
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string, size_t> word_count; //string到size_t的空map
string word;
while(cin >> word)
++word_count[word]; //提取word的计数器并将其加1
for(const auto &w : word_count)
cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << endl;
return 0;
}
本文介绍了一个使用C++编写的简单单词计数程序。该程序通过读取标准输入中的单词,并利用C++ STL中的map容器来记录每个单词出现的次数。最后,程序将输出每个单词及其出现的频次。
437

被折叠的 条评论
为什么被折叠?



