56. Select algorithm

The select() function that you have to implement takes an std::vector<T> as an input argument and a function of type F and returns a std::vector<R> as the result, where R is the result of applying F to T. We could use std::result_of() to deduce the return type of an invoke expression at compile time. Internally, the select() function should use std::transform() to iterate over the elements of the input vector, apply function f to each element, and insert the result in an output vector.

The following listing shows the implementation for this function:

template <   typename T, typename A, typename F,   typename R = typename std::decay<typename std::result_of<                typename std::decay<F>::type&( typename std::vector<T, A>::const_reference)>::type>::type> ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.