The question is quite dumb, but I need to do it in a very efficient way - it will be performed over an over again in my code. I have a function that returns a vector, and I have to add the returned values to another vector, element by element. Quite simple:
vector<double> result; vector<double> result_temp for(int i=0; i< 10; i++) result_temp.push_back(i); result += result_temp //I would like to do something like that. for(int i =0; i< result_temp.size();i++)result[i] += result_temp[i]; //this give me segfault
The mathematical operation that I'm trying to do is
u[i] = u[i] + v[i] for all i
What can be done?
Thanks
EDIT: added a simple initialization, as that is not the point. How should result be initialized?
The simplest solution is to use a copy constructor to initialize the target vector with the copy of all the first vector elements. Then, call the vector::insert function to copy all elements of the second vector. We can also use only vector::insert to copy elements of both vectors into the destination vector.
Given two vectors, join these two vectors using STL in C++. Approach: Joining can be done with the help of set_union() function provided in STL. Syntax: set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);
Use the insert Function to Append Vector to Vector in C++ The insert method is a built-in function of the std::vector container that can add multiple elements to the vector objects.
vector insert() function in C++ STLstd::vector::insert() is a built-in function in C++ STL which inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.
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
.
If you are trying to append one vector
to another, you can use something like the following. These are from one of my utilities libraries--two operator+=
overloads for std::vector
: one appends a single element to the vector
, the other appends an entire vector
:
template <typename T> std::vector<T>& operator+=(std::vector<T>& a, const std::vector<T>& b) { a.insert(a.end(), b.begin(), b.end()); return a; } template <typename T> std::vector<T>& operator+=(std::vector<T>& aVector, const T& aObject) { aVector.push_back(aObject); return aVector; }
If you are trying to perform a summation (that is, create a new vector
containing the sums of the elements of two other vector
s), you can use something like the following:
#include <algorithm> #include <functional> template <typename T> std::vector<T> operator+(const std::vector<T>& a, const std::vector<T>& b) { assert(a.size() == b.size()); std::vector<T> result; result.reserve(a.size()); std::transform(a.begin(), a.end(), b.begin(), std::back_inserter(result), std::plus<T>()); return result; }
You could similarly implement an operator+=
overload.
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