Thursday, 7 October 2021

Chapter 21 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Exercise 8

Take the word-frequency example from section 21.6.1 and modify it to output its lines in order of frequency (rather than in lexicographical order). An example would be 3: C++ rather than C++: 3.


This one was tricky as you can't really sort a map and you can't insert based on value because if a value becomes the highest frequency then we need to move it to the top. Instead you need to create a second temporary container and add your values to that, sort it how you want and then use the data. I haven't figured out how to put the data back into the map in that order as it will sort by the key when inserting again. Maps are confusing.

No comments:

Post a Comment