16.1 State whether each of the following is true or false. If false, explain why.
Standard Library algorithms can operate on C-like pointer-based arrays.
Standard Library algorithms are encapsulated as member functions within each container class.
When using the remove
algorithm on a container, the algorithm does not decrease the size of the container from which elements are being removed.
One disadvantage of using Standard Library algorithms is that they depend on the implementation details of the containers on which they operate.
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.
The find_if_not
algorithm locates all the values in the range for which the specified unary predicate function returns false
.
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).
16.2 Fill in the blanks in each of the following statements:
Standard Library algorithms operate on container elements indirectly, using .
The sort
algorithm requires a(n) iterator.
Algorithms and set every element in a range of container elements to a specific value.
The algorithm compares two sequences of values for equality.
The C++11 algorithm locates both the smallest and largest elements in a range.
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.
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.
16.3 Write a statement to peform each of the following tasks:
Use the fill
algorithm to fill the entire array
of string
s named items
with "hello"
.
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 int
s named integers
.
Use the equal
algorithm to compare two lists (strings1
and strings2
) for equality. Store the result in bool
variable result
.
Use the remove_if
algorithm to remove from the vector
of string
s 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
.
Use the replace_if
algorithm to replace with 0
all elements with values greater than 100
in the array
of int
s named values
. Function greaterThan100
returns true
if its argument is greater than 100
.
Use the minmax_element
algorithm to find the smallest and largest values in the array
of double
s named temperatures
. Store the pair
of iterators that’s returned in result
.
Use the sort
algorithm to sort the array
of string
s named colors
.
Use the reverse
algorithm to reverse order of the elements in the array
of string
s named colors
.
Use the merge
algorithm to merge the contents of the two sorted array
s named values1
and values2
into a third array
named results
.
Write a lambda expression that returns the square of its int
argument and assign the lambda expression to variable squareInt
.