11章习题

11.2统计单词数

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<sstream>
#include<algorithm>
using namespace std;
//单词统计
int main(int argc,char *argv[]){
    ifstream in("打开文件的绝对路径");
    if (!in){
        cout << "open failed!" << endl;
        exit(1);
    }

    map<string, size_t>word_count;
    string word;
    while (in >> word)
        word_count[word]++;
    for (const auto&w : word_count)
        cout << w.first << " occours " << w.second << ((w.second > 1) ? " times" : " time") << endl;
    system("pause");
    return 0;
}

 

posted @ 2017-12-13 17:18  bananaa  阅读(124)  评论(0编辑  收藏  举报