C++ primer 第五版 中文版 练习 11.3
题目:编写你自己的单词计数程序
答:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, size_t> word_count;
string word;
while (cin >> word)
{
++word_count[word];
}
for (const auto &w:word_count)
cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << endl;
return 0;
}