I know I can do this y[i] += f(x[i]) using transform with two input iterators.
however it seems somewhat counterintuitive and more complicated than for loop.
Is there a more natural way to do so using existing algorithm in boost or Stl. I could not find clean equivalent.
here is transform (y = y + a*x):
using boost::lambda;
transform(y.begin(), y.end(), x.begin(), y.begin(), (_1 + scale*_2);
// I thought something may exist:
transform2(x.begin(), x.end(), y.begin(), (_2 + scale*_1);
// it does not, so no biggie. I will write wrapper
Thanks
There are several ways to do this.
As you noted you can use
transformwith a number of predicates, some more or less automatically generated:Now, if we had a
vectorwith the range of indices, we could do it in a more “pythony” way:I don’t know if there could be something to do with views, they normally allow some pretty syntax. Of course it’s a bit difficult here I think because of the self-modifying
y.