4.18. Counting Instances of Each Word in a Text File
Problem
You want to count the number of occurrences of each word in a text file.
Solution
Use operator>>, defined in <string>, to read contiguous chunks of text from the
input file, and use a map, defined in <map>, to store each word and its frequency in the file.
Example 4-27 demonstrates how to do
this.
Example 4-27. Counting word frequencies
1 #include <iostream>
2 #include <fstream>
3 #include <map>
4 #include <string>
5
6 typedef std::map<std::string, int> StrIntMap;
7
8 void countWords(std::istream& in, StrIntMap& words) {
9
10 std::string s;
11
12 while (in >> s) {
13 ++words[s];
14 }
15 }
16
17 int main(int argc, char** argv) {
18
19 if (argc < 2)
20 return(EXIT_FAILURE);
21
22 std::ifstream in(argv[1]);
23
24 if (!in)
25 exit(EXIT_FAILURE);
26
27 StrIntMap w;
28 countWords(in, w);
29
30 for (StrIntMap::iterator p = w.begin();
31 p != w.end(); ++p) {
32 std::cout << p->first << " occurred "
33 << p->second << " times.\n";
34 }
35 }Discussion
Example 4-27 looks simple enough, but
there is more going on than it appears. Most of the subtleties have to do with maps, so let’s talk about them first.
If you’re not familiar with maps, you should be. A
map is a container class template that is part of the
STL. It stores key-value pairs in order according to std::less, or your custom comparison function, should you supply one. The kinds of keys and values you can store in it depend only on your imagination; in this example, we are just ...