Exercise Sheet 4

IN3013/INM173 - Object-Oriented Programming in C++

These exercises provide practice with using and defining template functions and classes.

  1. A queue is a collection to which items may be added or removed, such that the item removed is always the first one added (and still there). Write a generic queue class based on list and test it.

  2. Write a program that reads words from the standard input and when the input is reached prints out each word once, with a count of the number of times it occurred in the input. You will need a map from strings to integers to keep track of the number of occurrences of each word. Because there is as yet no easy way to get all the words in a map, you will also need to maintain a vector or list of all the words seen (but only one of each). (Next week we will cover iterators, which can be used instead.)

  3. Write a new program that reads a series of lines, each comprising a word and an associated number, and then prints out for each word the total and average of the associated numbers. You will need a new class to hold the statistics for a word, and then a map from words to objects of this class.