Meeting 05: Review of Data Structures: A Tour of C++ Standard Library

TIP: prefer to use containers and algorithms from the standard library rather than hand-coded data structures and algorithms. Learn what is in the standard library and how to use it.

In 2574 you saw how to implement data structures and common algorithms for sorting and searching. However, the C++ standard library provides implementations of these that are efficient and well tested, so you should prefer to use them over hand-coded approaches whenever feasible.

Aside: This might cause you some annoyance, why did we ask you to write code in 2574 that already exists? The reasons are that: one, it helped you understand which data structure and algorithm to use and under what tradeoffs, two, it was good experience, and three, in some cases, often in embedded programming, the C++ standard library is not a viable option.

This extends beyond complex algorithms, to what might appear as simple loops.

Links:

Exercise 05: Word Count

The goal of this exercise is to learn how to look for useful parts of the standard library and how to use it generally.

GitHub Invitation URL: exercise5

Prerequisites:

Steps:

  1. Create a working directory somewhere on your computer, then change to that directory.
  2. Clone the assignment for today after accepting the GitHub invitation at the link above.

    git clone https://github.com/VTECE3574/exercise05-USER.git

    where USER is your GitHub username. You will have to enter your GitHub username and password.

  3. In a file named wordcount.cpp write a program that reads all the words from a text file name "prose.txt", counting how many times each word appears, sorting them from the most frequently appearing to least, then writing to standard out the top 10 most frequently used words.

    • Consider a word any space-separated string.
    • Leverage the standard library as much as possible.
    • For the purpose of this exercise you can skimp on error checking.
  4. Now, use git to commit the source file you added to the local repository.
  5. Finally, use git push to synchronize the repository with that on GitHub

You have completed the Exercise.