I’m trying to implement an Euclidean vector for my programming assignment. I need to overload operator* to provide dot product calculation for two vectors with arbitrary same dimension.
As 3D vector for example:
Vector<3> v1, v2; //two 3D vectors.
double dotProduct = v1 * v2;
The value of dotProduct should be v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]
So, my problem is how to get this value without using any explicit loop and std::accumulate() operation in numeric.h header file? Because those are forbidden in this assignment.
P.S. I may use functor(self defined) together with STL algorithm.
If you really want to avoid explicit loops and algorithms in general (not just
std::accumulate), you could usestd::valarrays instead:I’ve used
doubleas the type here, but you can (of course) use whatever type makes sense for the data/situation you’re dealing with.