Drill for Chapter 21 on page 798.
After each operation (as defined by a line of this drill) print the vector
.
struct Item { string name; int iid; double value; /* . . . */ };
, make
a vector<Item>
, vi
, and fill it with ten items from a file.vi
by name.vi
by iid
.vi
by value; print it in order of decreasing value (i.e., largest value
first).Item("horse shoe",99,12.34)
and Item("Canon S400", 9988,499.95)
.Item
s identified by name from vi
.Item
s identified by iid
from vi
.list<Item>
rather than a vector<Item>
.Now try a map
:
map<string,int>
called msi
.(name,value)
pairs into it, e.g., msi["lecture"]=21
.(name,value)
pairs to cout
in some format of your choice.(name,value)
pairs from msi
.cin
and places them in msi
.msi
.msi
to cout
.msi
.map<int,string>
called mis
.msi
into mis
; that is, if msi
has an element
( "lecture",21 )
, mis
should have an element ( 21,"lecture" )
.mis
to cout
.More vector
use:
vector<double>
called vd
.vd
to cout
.vi
of type vector<int>
with the same number of elements
as vd
; copy the elements from vd
into vi
.( vd[i] , vi[i] )
to cout
, one pair per line.vd
.vd
and the sum
of the elements of vi
.vd
, and output vd
to cout
.vd
; output it.vector<double>
called vd2
and copy all elements of vd
with
values lower than (less than) the mean into vd2
.vd
; output it again.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 Fruit
s into the set
. What
if we didn’t want to copy the Fruit
s? 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 Order
s to and from a file. Define a mechanism for
printing Order
s. Create a file of at least ten Order
s, 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 Order
s 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
.