Exercises

  1. 16.4 State whether each of the following is true or false. If false, explain why.

    1. Because Standard Library algorithms process containers directly, one algorithm can often be used with many different containers.

    2. Use the for_each algorithm to apply a general function to every element in a range; for_each does not modify the sequence.

    3. By default, the sort algorithm arranges the elements in a range in ascending order.

    4. Use the merge algorithm to form a new sequence by placing the second sequence after the first.

    5. Use the set_intersection algorithm to find the elements from a first set of sorted values that are not in a second set of sorted values (both sets of values must be in ascending order).

    6. Algorithms lower_bound, upper_bound and equal_range are often used to locate insertion points in sorted sequences.

    7. Lambda expressions can also be used where function pointers and function objects are used in algorithms.

    8. C++11’s lambda expressions are defined locally inside functions and can “capture” (by value or by reference) the local variables of the enclosing function then manipulate these variables in the lambda’s body.

  2. 16.5 Fill in the blanks in each of the following statements:

    1. As long as a container’s (or built-in array’s)           satisfy the requirements of an algorithm, the algorithm can work on the container.

    2. Algorithms generate and generate_n use a(n)           function to create values for every element in a range of container elements. That type of function takes no arguments and returns a value that can be placed in an element of the container.

    3. Pointers into built-in arrays are           iterators.

    4. Use the           algorithm (the template of which is in header <numeric>) to sum the values in a range.

    5. Use the           algorithm to apply a general function to every element in a range when you need to modify those elements.

    6. In order to work properly, the binary_search algorithm requires that the sequence of values must be          .

    7. Use the function iter_swap to exchange the elements that are pointed to by two           iterators and exchanges the values in those elements.

    8. C++11’s minmax algorithm receives two items and returns a(n)           in which the smaller item is stored in first and the larger item is stored in second.

    9.           algorithms modify the containers they operate on.

  3. 16.6 List several advantages function objects provide over function pointers.

  4. 16.7 What happens when you apply the unique algorithm to a sorted sequence of elements in a range?

  5. 16.8 (Duplicate Elimination) Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values.

  6. 16.9 (Duplicate Elimination) Modify Exercise 16.8 to use the unique_copy algorithm. The unique values should be inserted into a vector that’s initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.

  7. 16.10 (Reading Data from a File) Use an istream_iterator<int>, the copy algorithm and a back_inserter to read the contents of a text file that contains int values separated by whitespace. Place the int values into a vector of ints. The first argument to the copy algorithm should be the istream_iterator<int> object that’s associated with the text file’s ifstream object. The second argument should be an istream_iterator<int> object that’s initialized using the class template istream_iterator’s default constructor—the resulting object can be used as an “end” iterator. After reading the file’s contents, display the contents of the resulting vector.

  8. 16.11 (Merging Ordered Lists) Write a program that uses Standard Library algorithms to merge two ordered lists of strings into a single ordered list of strings, then displays the resulting list.

  9. 16.12 (Palindrome Tester) A palindrome is a string that is spelled the same way forward and backward. Examples of palindromes include “radar” and “able was i ere i saw elba.” Write a function palindromeTester that uses the reverse algorithm on an a copy of a string, then compares the original string and the reversed string to determine whether the original string is a palindrome. Like the Standard Library containers, string objects provide functions like begin and end to obtain iterators that point to characters in a string. Assume that the original string contains all lowercase letters and does not contain any punctuation. Use function palindromeTester in a program.

  10. 16.13 (Enhanced Palindrome Tester) Enhance Exercise 16.12’s palindromeTester function to allow strings containing uppercase and lowercase letters and punctuation. Before testing whether the original string is a palindrome, function palindromeTester should convert the string to lowercase letters and eliminate any punctuation. For simplicity, assume the only punctuations characters can be

    
    . , ! ; : ()
    

    You can use the copy_if algorithm and a back_inserter to make a copy of the original string, eliminate the punctuation characters and place the characters into a new string object.

  11. 16.14 Explain why using the “weakest iterator” that yields acceptable performance helps produce maximally reusable components.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset