MFC Programmer's SourceBook : Thinking in C++
Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

Exercises

  1. 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( ).
  2. Modify Stlshape.cpp from chapter XXX so that it uses transform( ) to delete all its objects.
  3. Using transform( ) and toupper( ) (in <cctype>) write a single function call that will convert a string to all uppercase letters.
  4. Create a Sum function object template that will accumulate all the values in a range when used with for_each( ).
  5. Write an anagram generator that takes a word as a command-line argument and produces all possible permutations of the letters.
  6. 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).
  7. Create a class hierarchy with a base class B and a derived class D. Put a virtual member function void f( ) 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.
  8. Modify FunctionObjects.cpp so that it uses float instead of int.
  9. 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).
  10. 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.
  11. 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.
  12. 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( ).
  13. 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.
  14. 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.
  15. Create template function objects that perform bitwise operations for &, |, ^ and ~. Test these with a bitset.
  16. 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>).
  17. 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.
  18. 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.
  19. Create and test a logical_xor function object template to implement a logical exclusive- or.
  20. Create an STL-style algorithm transform_if( ) following the first form of transform( ) which only performs transformations on objects that satisfy a unary predicate.
  21. 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.
  22. 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.
  23. Templatize the Matrix class and associated operations from the previous example so they will work with any appropriate type.
Contents | Prev | Next


Go to CodeGuru.com
Contact: webmaster@codeguru.com
© Copyright 1997-1999 CodeGuru