How to add element by element of two STL vectors?

It sure looks like the problem is accessing values of result that don’t exist. tzaman shows how to initialize result to 10 elements, each with value 0.

Now you need to call the transform function (from <algorithm>), applying the plus function object (from <functional>):

std::transform(result.begin(), result.end(), result_temp.begin(),
               result.begin(), std::plus<double>());

This iterates over result and result_temp, applies plus that adds doubles, and writes the sum back to result.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)