DM560, Introduction to Programming in C++

Sheet 12

Task 1

Drill for Chapter 21 on page 798.

After each operation (as defined by a line of this drill) print the vector.

  1. Define a struct Item { string name; int iid; double value; /* . . . */ };, make a vector<Item>, vi, and fill it with ten items from a file.
  2. Sort vi by name.
  3. Sort vi by iid.
  4. Sort vi by value; print it in order of decreasing value (i.e., largest value first).
  5. Insert Item("horse shoe",99,12.34) and Item("Canon S400", 9988,499.95).
  6. Remove (erase) two Items identified by name from vi.
  7. Remove (erase) two Items identified by iid from vi.
  8. Repeat the exercise with a list<Item> rather than a vector<Item>.

Now try a map:

  1. Define a map<string,int> called msi.
  2. Insert ten (name,value) pairs into it, e.g., msi["lecture"]=21.
  3. Output the (name,value) pairs to cout in some format of your choice.
  4. Erase the (name,value) pairs from msi.
  5. Write a function that reads value pairs from cin and places them in msi.
  6. Read ten pairs from input and enter them into msi.
  7. Write the elements of msi to cout.
  8. Output the sum of the (integer) values in msi.
  9. Define a map<int,string> called mis.
  10. Enter the values from msi into mis; that is, if msi has an element ( "lecture",21 ), mis should have an element ( 21,"lecture" ).
  11. Output the elements of mis to cout.

More vector use:

  1. Read some floating-point values (at least 16 values) from a file into a vector<double> called vd.
  2. Output vd to cout.
  3. Make a vector vi of type vector<int> with the same number of elements as vd; copy the elements from vd into vi.
  4. Output the pairs of ( vd[i] , vi[i] ) to cout, one pair per line.
  5. Output the sum of the elements of vd.
  6. Output the difference between the sum of the elements of vd and the sum of the elements of vi.
  7. There is a standard library algorithm called reverse that takes a sequence (pair of iterators) as arguments; reverse vd, and output vd to cout.
  8. Compute the mean value of the elements in vd; output it.
  9. Make a new vector<double> called vd2 and copy all elements of vd with values lower than (less than) the mean into vd2.
  10. Sort vd; output it again.

Task 2

Do exercises 2, 6, 7, 9 and 10 of Chapter 21 on page 801.

  • Find a reliable source of STL documentation and list every standard library algorithm.

  • In the Fruit example in §21.6.5, we copy Fruits into the set. What if we didn’t want to copy the Fruits? We could have a set<Fruit*> instead. However, to do that, we’d have to define a comparison operation for that set. Implement the Fruit example using a set<Fruit*, Fruit_comparison>. Discuss the differences between the two implementations.

  • Write a binary search function for a vector<int> (without using the standard one). You can choose any interface you like. Test it. How confident are you that your binary search function is correct? Now write a binary search function for a list<string>. Test it. How much do the two binary search functions resemble each other? How much do you think they would have resembled each other if you had not known about the STL?

  • Define an Order class with (customer) name, address, data, and vector<Purchase> members. Purchase is a class with a (product) name, unit_price, and count members. Define a mechanism for reading and writing Orders to and from a file. Define a mechanism for printing Orders. Create a file of at least ten Orders, read it into a vector<Order>, sort it by name (of customer), and write it back out to a file. Create another file of at least ten Orders of which about a third are the same as in the first file, read it into a list<Order>, sort it by address (of customer), and write it back out to a file. Merge the two files into a third using std::merge().

  • Compute the total value of the orders in the two files from the previous exercise. The value of an individual Purchase is (of course) its unit_price*count.