如何统计出一篇文章出现的文字个数? (中级) 使用std::vector
1
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : CountRepeatedWordByVector.cpp
5
Compiler : Visual C++ 8.0
6
Description : Demo how to count repeated words by vector
7
Release : 11/16/2006
8
*/
9
10
#include <iostream>
11
#include <fstream>
12
#include <cctype>
13
#include <string>
14
#include <vector>
15
#include <algorithm>
16
17
void repeatedWords(std::string);
18
19
int main() {
20
std::string fileName("textfile.txt");
21
repeatedWords(fileName);
22
23
return 0;
24
}
25
26
void repeatedWords(std::string fileName) {
27
28
std::ifstream infile(fileName.c_str());
29
30
if (!infile) {
31
std::cout << "Read error!!" << std::endl;
32
33
return;
34
}
35
36
std::vector<std::string> wordsVec;
37
38
for(std::string word, newWord; infile >> word; newWord = "") {
39
for(std::string::iterator iter = word.begin();
40
iter != word.end(); ++iter) {
41
42
if (!ispunct(*iter)) {
43
newWord.push_back(tolower(*iter));
44
}
45
}
46
47
wordsVec.push_back(newWord);
48
}
49
50
infile.close();
51
52
// unique_copy output vector must be initialized first
53
std::vector<std::string> uniqueWordsVec(wordsVec.size());
54
// Must sort before using unique_copy
55
sort(wordsVec.begin(), wordsVec.end());
56
// unique_copy algorithm to uniqueWordsVec
57
std::vector<std::string>::const_iterator endIter = unique_copy(wordsVec.begin(), wordsVec.end(), uniqueWordsVec.begin());
58
59
for(std::vector<std::string>::const_iterator iter = uniqueWordsVec.begin(); iter != endIter; ++iter) {
60
// count algorithm to count
61
std::cout << "The Word '" << *iter << "' Occurrence:" << (size_t)count(wordsVec.begin(), wordsVec.end(),*iter) << " times" << std::endl;
62
}
63
}
/* 2
(C) OOMusou 2006 http://oomusou.cnblogs.com3

4
Filename : CountRepeatedWordByVector.cpp5
Compiler : Visual C++ 8.06
Description : Demo how to count repeated words by vector7
Release : 11/16/20068
*/9

10
#include <iostream>11
#include <fstream>12
#include <cctype>13
#include <string>14
#include <vector>15
#include <algorithm>16

17
void repeatedWords(std::string);18

19
int main() {20
std::string fileName("textfile.txt");21
repeatedWords(fileName);22

23
return 0;24
}25

26
void repeatedWords(std::string fileName) {27

28
std::ifstream infile(fileName.c_str());29

30
if (!infile) {31
std::cout << "Read error!!" << std::endl;32

33
return;34
}35
36
std::vector<std::string> wordsVec;37

38
for(std::string word, newWord; infile >> word; newWord = "") {39
for(std::string::iterator iter = word.begin();40
iter != word.end(); ++iter) {41

42
if (!ispunct(*iter)) {43
newWord.push_back(tolower(*iter));44
}45
}46
47
wordsVec.push_back(newWord);48
}49

50
infile.close();51

52
// unique_copy output vector must be initialized first53
std::vector<std::string> uniqueWordsVec(wordsVec.size());54
// Must sort before using unique_copy55
sort(wordsVec.begin(), wordsVec.end());56
// unique_copy algorithm to uniqueWordsVec57
std::vector<std::string>::const_iterator endIter = unique_copy(wordsVec.begin(), wordsVec.end(), uniqueWordsVec.begin());58

59
for(std::vector<std::string>::const_iterator iter = uniqueWordsVec.begin(); iter != endIter; ++iter) {60
// count algorithm to count61
std::cout << "The Word '" << *iter << "' Occurrence:" << (size_t)count(wordsVec.begin(), wordsVec.end(),*iter) << " times" << std::endl;62
}63
}


浙公网安备 33010602011771号