[Solved] Get the dot product of two vectors by functors and STL algorithms

Just use std::inner_product: live demo #include <algorithm> #include <iostream> #include <iterator> #include <ostream> #include <numeric> #include <cstdlib> #include <vector> using namespace std; int main() { vector<int> v1,v2; generate_n( back_inserter(v1), 256, rand ); generate_n( back_inserter(v2), v1.size(), rand ); cout << inner_product( v1.begin(), v1.end(), v2.begin(), 0 ) << endl; } Yes, that’s a good solution. But I … Read more

[Solved] A function object:

A function object is an instance of a class that defines the parenthesis operator as a member function. When a function object is used as a function, the parenthesis operator is invoked whenever the function is called. Consider the following class definition: class biggerThanThree { public: bool operator () (int val) { return val > … Read more