Self-Review Exercises

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

    1. Standard Library algorithms can operate on C-like pointer-based arrays.

    2. Standard Library algorithms are encapsulated as member functions within each container class.

    3. When using the remove algorithm on a container, the algorithm does not decrease the size of the container from which elements are being removed.

    4. One disadvantage of using Standard Library algorithms is that they depend on the implementation details of the containers on which they operate.

    5. The remove_if algorithm does not modify the number of elements in the container, but it does move to the beginning of the container all elements that are not removed.

    6. The find_if_not algorithm locates all the values in the range for which the specified unary predicate function returns false.

    7. Use the set_union algorithm to create a set of all the elements that are in either or both of two sorted sets (both sets of values must be in ascending order).

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

    1. Standard Library algorithms operate on container elements indirectly, using          .

    2. The sort algorithm requires a(n)           iterator.

    3. Algorithms           and           set every element in a range of container elements to a specific value.

    4. The           algorithm compares two sequences of values for equality.

    5. The C++11           algorithm locates both the smallest and largest elements in a range.

    6. A back_inserter calls the container’s default           function to insert an element at the end of the container. If an element is inserted into a container that has no more space available, the container grows in size.

    7. Any algorithm that can receive a function pointer can also receive an object of a class that overloads the parentheses operator with a function named operator(), provided that the overloaded operator meets the requirements of the algorithm. An object of such a class is known as a(n)           and can be used syntactically and semantically like a function or function pointer.

  3. 16.3 Write a statement to peform each of the following tasks:

    1. Use the fill algorithm to fill the entire array of strings named items with "hello".

    2. Function nextInt returns the next int value in sequence starting with 0 the first time it’s called. Use the generate algorithm and the nextInt function to fill the array of ints named integers.

    3. Use the equal algorithm to compare two lists (strings1 and strings2) for equality. Store the result in bool variable result.

    4. Use the remove_if algorithm to remove from the vector of strings named colors all of the strings that start with "bl". Function startsWithBL returns true if its argument string starts with "bl". Store the iterator that the algorithm returns in newEnd.

    5. Use the replace_if algorithm to replace with 0 all elements with values greater than 100 in the array of ints named values. Function greaterThan100 returns true if its argument is greater than 100.

    6. Use the minmax_element algorithm to find the smallest and largest values in the array of doubles named temperatures. Store the pair of iterators that’s returned in result.

    7. Use the sort algorithm to sort the array of strings named colors.

    8. Use the reverse algorithm to reverse order of the elements in the array of strings named colors.

    9. Use the merge algorithm to merge the contents of the two sorted arrays named values1 and values2 into a third array named results.

    10. Write a lambda expression that returns the square of its int argument and assign the lambda expression to variable squareInt.

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

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