练习11.38:用unordered_map重写单词计数程序(参加11.1节,第375页)和单词转换程序(参见11.3.6节,第391页)。
/*
*C++Primer第五版
*练习11.38
*2015/10/14
*问题描述:练习11.38:用unordered_map重写单词计数程序(参加11.1节,第375页)和单词转换程序(参见11.3.6节,第391页)。
*说明:#include <tr1/unordered_map> 该头文件写法
*作者:Nick Feng
*邮箱:nickgreen23@163。com
*/
/*
*C++Primer(第五版)
*练习11.3
*2015/9/22
*问题描述:练习11.3:编写你自己的单词记数程序
*说明:照着P375页copy了一遍
*作者:Nick Feng
*邮箱;nickgreen23@163.com
*/
#include <iostream>
#include <string>
#include <map>
#include <tr1/unordered_map>
using namespace std;
int main()
{
//注意格式
std::tr1::unordered_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;
}