replace all odd values in vector with coresponing value from new vector
You can use the std::transform
overload that takes two input iterator ranges:
std::transform(foo.begin(), foo.end(), bar.begin(), foo.begin(),
[](auto const& a, auto const& b) {
if (a % 2)
return b;
return a;
}
);
Adding to the answers here you can make a function of your own:
Demo: https://godbolt.org/z/yf3jYx
void IsOdd (const std::vector<int>& a, std::vector<int>& b) {
for(size_t i = 0; i < a.size() && i < b.size(); i++){
if(b[i] % 2 == 1)
b[i] = a[i];
}
}
and call it in main:
IsOdd(bar, foo);