June 2017
Intermediate to advanced
532 pages
12h 59m
English
This recipe concentrates on collecting all words in an std::map and then shoves all items out of the map and into an std::vector, which is then sorted differently, in order to print the data. Why?
Let's look at an example. When we count the word frequency in the string "a a b c b b b d c c", we would get the following map content:
a -> 2b -> 4c -> 3d -> 1
However, that is not the order which we want to present to the user. The program should print b first because it has the highest frequency. Then c, then a, then d. Unfortunately, we cannot request the map to give us the "key with the highest associated value", then the "key with the second highest associated value", and so on.
Here, the vector comes into play. We typed ...
Read now
Unlock full access