When applying a function to a collection of elements, std::transform takes the output as 3rd parameter. Is there version which returns the result, something like vec2 = map(func, vec1)?
No, there is nothing like that in the standard library. You can write one yourself:
template<typename T, typename Func>
std::vector<T> transform(std::vector<T> const &input, Func func) {
std::vector<T> result(input.size());
std::transform(input.begin(), input.end(), result.begin(), func);
return result;
}
A better solution may be to use transformed adaptor from Boost.Range as it does not allocate additional container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With