The std::map
is very useful when categorizing something in order to collect statistics about that data. By attaching modifiable payload objects to every key which represents an object category, it is pretty simple to implement a histogram of word frequencies for example. This is what we will do in this section.
In this section, we will read all user input from standard input, which might, for example, be a text file containing an essay. We tokenize the input to words, in order to count which word occurs how often.
- As always, we need to include all the headers from the data structures we are going to use.
#include <iostream> #include <map> #include <vector> #include <algorithm> #include <iomanip>
- To spare us some typing, we declare that we use namespace
std
.
using namespace std;
- We will use one helper function in order to crop possibly appended commas, dots, or...