以下代碼将通過類來實作對指定文章的詞頻統計,并以指定格式輸出.
大家自行測試,如有錯誤歡迎指正.
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <fstream>
5 #include <sstream>
6 #include <algorithm>
7
8 using std::cout;
9 using std::endl;
10 using std::string;
11 using std::vector;
12 using std::ifstream;
13 using std::ofstream;
14 using std::istringstream;
15
16 const int initCnt = 10000;
17
18 struct Record
19 {
20 string word;
21 int cnt;
22 };
23
24 bool operator<(const Record & lhs,const Record & rhs)
25 {
26 return lhs.word < rhs.word;
27 }
28
29 class WordStastic
30 {
31 public:
32 WordStastic()
33 {
34 _dict.reserve(initCnt);
35 }
36
37 void readFile(const string & filename);
38 void writeFile(const string & filename);
39
40 private:
41 vector<Record> _dict;
42 };
43
44 void WordStastic::readFile(const string & filename)
45 {
46 ifstream ifs(filename);
47 if(!ifs.good())
48 {
49 cout << "Oops~" << endl;
50 return ;
51 }
52
53 string line;
54 while(getline(ifs,line))
55 {
56 istringstream iss(line);
57 string word;
58 while(iss >> word)
59 {
60 vector<Record>::iterator it;
61 for(it = _dict.begin();it!=_dict.end();++it)
62 {
63 if(word == it->word)
64 {
65 ++(it->cnt);
66 break;
67 }
68 }
69 if(it==_dict.end())
70 {
71 Record record;
72 record.word = word;
73 record.cnt = 1;
74 _dict.push_back(record);
75 }
76 }
77 }
78 ifs.close();
79 std::sort(_dict.begin(),_dict.end());
80 }
81
82 void WordStastic::writeFile(const string & filename)
83 {
84 ofstream ofs(filename);
85 if(!ofs.good())
86 {
87 cout << "Oops!" << endl;
88 return ;
89 }
90
91 for(auto & elem : _dict)
92 {
93 ofs << elem.word << " " << elem.cnt << endl;
94 }
95
96 ofs.close();
97 }
轉載于:https://www.cnblogs.com/m4ch0/p/7040269.html