Histogramming in C++ with Unordered Maps

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=Cly5O0Jfvt0



Duration: 51:00
214 views
1


Today we did an hour of lab time in which we wrote code to count how many times each word appears in a file. This is going to lead us in to our next assignment which will do a Bridges visualization using linked lists displaying the top 30 words used in Shakespeare plays.

The sticking points for most students were:
1) When you do a range-based for loop over an unordered_map, it returns a pair with the key in .first and the value in .second.
2) If you want to sort the key/value pairs by value (in this case count), there's a number of different ways to do it. The way I did in class wasn't perhaps optimal, but it works. I made a class holding the key and value, and then wrote an operator so that it would sort by value.
3) When erasing invalid characters from a string, it's easier and faster to build up a new string with good characters rather than using .erase and .find on the old string.







Tags:
csci 40
c++
shakespeare
histogram
histogramming
unordered_map