C++ Map 統計文件中的詞頻

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
   
using namespace std;
   
void display_map(map<string,int> &wmap);
   
int main()
{
    using namespace std;
    string filename;
    cout << "Enter the file name : ";
    cin >> filename;
    cin.get();
    ifstream fin(filename.c_str());                    //  read  in  str[]
    string  temp;
    map<string,int> wmap;
    while(fin>>temp)
        wmap[temp]++;
    display_map(wmap);
   
    fin.close();
    cin.get();
    return 0;
}
   
void display_map(map<string,int> &wmap)
{
    map<string,int>::const_iterator map_it;
    for(map_it=wmap.begin();map_it != wmap.end();map_it ++)
    {
        cout << "(\"" << map_it->first << "\"," << map_it->second << ")" << endl;
    }
}



發佈了25 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章