Resources‎ > ‎Programming‎ > ‎C++‎ > ‎C++ Tutorial‎ > ‎

Lesson 12

Practice 1

The first task is to define a function object class Show, which displays an element specified as its argument.class Show should not inherit anything from any other class.

Once you define class Show, write main() function to test it. You should be able to use Show function object like:

Show<string> myShow;
Show<double> myShowd;

Practice 2

Next, task is to implement class Show as a subclass of unary_function struct. You can define Show as a struct or a class.

Practice 3

Now you have created Show function object to display an element. Your next task is to create an algorithmout(), which takes two iterators (first and last) and display each element using your Show function object.

Practice 4

Using the above out() algorithm, which uses your struct Show function object, do the following:

  1. first store the following string array into vector<sring> vec,

    string strarray[] = {"a", "c", "l", "u", "w", "e", "c",
        "z", "b", "l", "o", "r"};
    int size = sizeof strarray / sizeof(string);
  1. next, display all elements in the vec using your out() algorithm,

  2. using sort() algorithm, which takes two arguments (vec.begin() and vec.end()), sort the elements invec and display them using out(),

  3. using sort() algorithm, which takes three arguments (vec.begin()vec.end() and greater function object), sort the elements in vec and display them using out().

Sample Solutions
Subpages (1): Sample Solutions