In the call to equal on rosters, what would happen if both rosters held C-style strings, rather than library strings?
For such case, std::equal is going to compare the address value rather than the string value. So the result is not the same as std::string. Try to avoid coding this way. Check #227 for more detail.
We said that algorithms do not change the size of the containers over which they operate. Why doesn’t the use of back_inserter invalidate this claim?
Inserters like back_inserter
is part of <iterator>
rather than <algorithm>
.
Why do you think the algorithms don’t change the size of containers?
@Mooophy: The aim of this design is to separate the algorithms and the operation provided by member function.
@pezy: Cause the library algorithms operate on iterators, not containers. Thus, an algorithm cannot (directly) add or remove elements.
Write a lambda that takes two ints and returns their sum.
auto add = [](int lhs, int rhs){ return lhs + rhs; };
Write a lambda that captures an int from its enclosing function and takes an int parameter. The lambda should return the sum of the captured int and the int parameter.
int i = 42;
auto add = [i](int num){ return i + num; };
How many arguments does bind take?
Assuming the function to be bound have n
parameters, bind take n + 1
parameters.
The additional one is for the function to be bound itself.
Explain the differences among the three kinds of insert iterators.
back_inserter
uses push_back
.front_inserter
uses push_front
.insert
uses insert
This function takes a second argument, which must be an iterator into the given container. Elements are inserted ahead of the element denoted by the given iterator.
List the five iterator categories and the operations that each supports.
==
, !=
, ++
, *
, ->
++
, *
==
, !=
, ++
, *
, ->
==
, !=
, ++
, --
, *
, ->
==
, !=
, <
, <=
, >
, >=
, ++
, --
, +
, +=
, -
, -=
, -
(two iterators), *
, ->
, iter[n]
== * (iter + n)
What kind of iterator does a list have? What about a vector?
list
have the Bidirectional iterators. vector
have the Random-access iterators.
What kinds of iterators do you think copy requires? What about reverse or unique?
copy
: first and second are Input iterators, last is Output iterators.reverse
: Bidirectional iterators.unique
: Forward iterators.Based only on the algorithm and argument names, describe the operation that the each of the following library algorithms performs:
replace(beg, end, old_val, new_val); // replace the old_elements in the input range as new_elements. replace_if(beg, end, pred, new_val); // replace the elements in the input range which pred is true as new_elements. replace_copy(beg, end, dest, old_val, new_val); // copy the new_elements which is old_elements in the input range into dest. replace_copy_if(beg, end, dest, pred, new_val); // copy the new_elements which pred is true in the input range into dest.