A word frequency counter with map
This recipe uses the unique key property of the map
container to count duplicate words from a stream of text.
The STL map
container is an associative container. It consists of elements organized in key-value pairs. The keys are used for lookup and must be unique.
In this recipe, we will leverage the unique key requirement of the STL map
container to count the number of occurrences of each word in a text file.
How to do it…
There are a few parts to this task that we can solve separately:
- We need to get the text from a file. We'll use the
cin
stream for this. - We need to separate words from punctuation and other non-word content. We'll use the
regex
(Regular Expression) library for this. - We need to count the frequency of each word. This is the main objective of the recipe. We'll use the STL
map
container for this. - Finally, we need to sort the results, first by frequency and then alphabetically by word...