A Word Frequency Counter with Map

Learn to create a word frequency counter with a map.

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:

  1. We need to get the text from a file. We'll use the cin stream for this.

  2. We need to separate words from punctuation and other non-word content. We'll use the regex (Regular Expression) library for this.

  3. 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.

  4. 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 sort algorithm with a vector container.

Even with all those tasks, the resulting code is relatively short, just about 7070 lines with headers and all. Let's dive in:

  • We'll start with some aliases for convenience:

Get hands-on with 1200+ tech skills courses.