forked from orazaro/accelerated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-05-wordcount.cpp
More file actions
44 lines (38 loc) · 950 Bytes
/
04-05-wordcount.cpp
File metadata and controls
44 lines (38 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <string>
struct WordCount {
std::string word;
int count;
};
typedef std::vector<WordCount>::size_type vec_sz;
int read(std::istream& in, std::vector<WordCount>& vec)
{
std::string word;
int words = 0;
while(in >> word) {
words++;
bool found = false;
for(vec_sz i = 0; i != vec.size(); ++i)
if(vec[i].word == word) {
vec[i].count ++;
found = true;
break;
}
if(!found) {
WordCount wc = {word,1};
vec.push_back(wc);
}
}
return words;
}
int main()
{
std::vector<WordCount> vec;
int words = read(std::cin, vec);
std::cout << "Result:" << std::endl;
for(vec_sz i = 0; i != vec.size(); ++i)
std::cout << vec[i].word << " " << vec[i].count << std::endl;
std::cout << "sum=" << words << std::endl;
return 0;
}