CodeBlocks
· Frequently in programming the concept of a "pair", two values rolled up into a single object, is useful; for example, a point can be considered a pair of a double and a double representing the x and y coordinates; a student's name and grade on a test can be expressed with a pair of a string and an int; and so forth.
First, create a class Pair in header and cpp files that has two template typename parameters, S and T, representing the data type of the first value and the second respectively. (For example, one might declare a variable of type Pair<double, double> for a point or a Pair<string, int> to hold a student's name and grade.) Give your Pair class accessors and mutators for the first value (of type S) and the second (of type T); also overload the ostream << operator for your Pair class to print the pair out in the form "(first, second)".
Having done that, create a second class, Cities in more header and cpp files, that stores a list of Pair<string, int> with each pair representing the name and population of a city. (I recommend using the Vector class that is attached). Your Cities class should have the following member functions:
- void add(const Pair<string, int> &city); — adds the given city (pair) to the list
- const Pair<string, int> & getCity(int i) const; — returns the city (pair) in the list at index i (0 being the first as usual)
- int getSize() const; — returns the number of cities in the list
- const Pair<string, int> & getLargestCity() const; — returns the city (pair) that has the largest population
- (and any others that you find useful for completing the rest of the assignment)
· Finally, write a program that reads in a text file with the populations and names of cities (a sample is attached), constructs a Pair<string, int> for each city, and adds it to a Cities object; your program should then (using your Cities object) print out a table of all the cities and their populations, as well as report which city had the greatest population. (Hint: cities can have spaces in their names, so use getline() to read the city name.)
12 years ago
Purchase the answer to view it

- cities_pair_vector_cpp.zip