A Word Frequency Counter with Map
Explore how to implement a word frequency counter by leveraging the STL map container's unique key feature in C++20. Learn to extract words using regex, count occurrences while ignoring case, and sort results by frequency and alphabetically, gaining practical skills in associative containers, algorithms, and input handling.
We'll cover the following...
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
cinstream 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
mapcontainer for this.Finally, we need to sort the results, first by frequency and then alphabetically by word within frequency. For this we'll use a the STL
sortalgorithm with avectorcontainer.
Even with all those tasks, the resulting code is relatively short, just about