Create
a generator that returns the current value of
clock( )
(in
<ctime>).
Create a
list<clock_t>
and fill it with your generator using
generate_n( ).
Remove any duplicates in the list and print it to
cout
using
copy( ).
Modify
Stlshape.cpp
from chapter XXX so that it uses
transform( )
to delete all its objects.
Using
transform( )
and
toupper( )
(in
<cctype>)
write a single function call that will convert a
string
to all uppercase letters.
Create
a
Sum
function object template that will accumulate all the values in a range when
used with
for_each( ).
Write
an anagram generator that takes a word as a command-line argument and produces
all possible permutations of the letters.
Write
a “sentence anagram generator” that takes a sentence as a
command-line argument and produces all possible permutations of the words in
the sentence (it leaves the words alone, just moves them around).
Create
a class hierarchy with a base class
B
and a derived class
D.
Put a
virtual
member function
voidf( )
in
B
such that it will print a message indicating that
B’s
f( )
has been called, and redefine this function for
D
to print a different message. Create a
deque<B*>
and fill it with
B
and
D
objects. Use
for_each( )
to call
f( )
for each of the objects in your
deque.
Modify
FunctionObjects.cpp
so that it uses
float
instead of
int.
Modify
FunctionObjects.cpp
so that it templatizes the main body of tests so you can choose which type
you’re going to test (you’ll have to pull most of
main( )
out into a separate template function).
Using
transform( ),
toupper( )
and
tolower( )
(in
<ccytpe>),
create two functions such that the first takes a
string
object and returns that
string
with all the letters in uppercase, and the second returns a
string
with
all the letters in lowercase.
Write
a program that takes as a command line argument the name of a text file. Open
this file and read it a word at a time (hint: use
>>).
Store each word into a
deque<string>.
Force all the words to lowercase, sort them, remove all the duplicates and
print the results.
Write
a program that finds all the words that are in common between two input files,
using
set_intersection( ).
Change it to show the words that are not in common, using
set_symmetric_difference( ).
Create
a program that, given an integer on the command line, creates a
“factorial table” of all the factorials up to and including the
number on the command line. To do this, write a generator to fill a
vector<int>,
then use
partial_sum( )
with a standard function object.
Modify
CalcInventory.cpp
so that it will find all the objects that have a quantity that’s less
than a certain amount. Provide this amount as a command-line argument, and use
copy_if( )
and
bind2nd( )
to create the collection of values less than the target value.
Create
template function objects that perform bitwise operations for
&,
|,
^
and
~.
Test these with a
bitset.
Fill
a
vector<double>
with numbers representing angles in radians. Using function object composition,
take the sine of all the elements in your vector (see
<cmath>).
Create
a
map
which is a cosine table where the keys are the angles in degrees and the values
are the cosines. Use
transform( )
with
cos( )
(in
<cmath>)
to fill the table.
Write
a program to compare the speed of sorting a
list
using
list::sort( )
vs. using
std::sort( )
(the STL algorithm version of
sort( )).
Hint: see the timing examples in the previous chapter.
Create
and test a
logical_xor
function object template to implement a logical exclusive-
or.
Create
an STL-style algorithm
transform_if( )
following the first form of
transform( )
which
only performs transformations on objects that satisfy a unary predicate.
Create
an STL-style algorithm which is an overloaded version of
for_each( )
that follows the second form of
transform( )
and takes two input ranges so it can pass the objects of the second input range
a to a binary function which it applies to each object of the first range.
Create
a
Matrix
class which is made from a
vector<vector<int>
>
.
Provide it with a
friend
ostream& operator<<(ostream&, const Matrix&)
to display the matrix. Create the following using the STL algorithms where
possible (you may need to look up the mathematical meanings of the matrix
operations if you don’t remember them):
operator+(const
Matrix&, const Matrix&)
for
Matrix
addition,
operator*(const
Matrix&, const vector<int>&)
for multiplying a matrix by a vector, and
operator*(const
Matrix&, const Matrix&)
for matrix multiplication. Demonstrate each.
Templatize
the
Matrix
class and associated operations from the previous example so they will work
with any appropriate type.