So, one of my classes has the following overload:
DVector &operator+=(DVector const &other) {
if (vector.size() >= other.vector.size()) throw up; // lol.
std::for_each(other.vector.begin(); other.vector.end(), [](DVector const &pass) {
// huh?
});
}
So, the idea is to sum every member of both vectors (uhm, every DVector instance contains a std::vector<float> member called vector), for example:
If I had one DVector containing a vector member containing the following floats: 11.0, 23.5, 12.3, and then another one containing 14.0, 6.5, 7.7, the sum of both should lead to the first vector holding 25.0, 25.0, 25.0.
The question: is there any way to loop through both vectors and sum their members using only one iterator, assuming that the size of the vectors isn’t a problem, or am I just forced to use a for (auto x: vector.size())?
Cheers, Julian.
The is a std::transfom algorithm that take in two inputs and an output iterator.
I don’t know how you specific vector design is consturcted but you should be able to modify the following for your needs.
You can also have a different output iterator for you
operator +also well.Here are some references
http://en.cppreference.com/w/cpp/algorithm/transform
http://www.cplusplus.com/reference/algorithm/transform/
And when you have a different output a back_inserter is your friend:
http://en.cppreference.com/w/cpp/iterator/back_inserter