Consider
struct foo{};
struct bar{
bar(const foo& f){}
};
And imagine that I have a
std::vector<foo> vec;
and I want to convert this to a std::vector<bar> out
. I can use
std::copy(vec.begin(), vec.end(), std::back_inserter(out));
to do that since bar
is not explicit
. However, I need bar
to be explicit
! But then the back_inserter
no longer works. What changes do I need to make to the std::copy
parameters to somehow include the explicit bar(<iteratee>)
?
std::transform(
vec.begin(),
vec.end(),
std::back_inserter(out),
[](const foo& f){
return bar(f);
}
);
will do it.
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