fork download
  1.  
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iostream>
  5.  
  6. double dot(const std::vector<double>& p, const std::vector<double>& q)
  7. {
  8. return std::inner_product(p.begin(), p.end(), q.begin(), 0);
  9. }
  10.  
  11. template<std::size_t N>
  12. double dot(const double (&p)[N], const double (&q)[N])
  13. {
  14. return std::inner_product(p, p+N, q, 0);
  15. }
  16.  
  17. int main()
  18. {
  19. std::vector<double> v1 = {1,2,3};
  20. std::vector<double> v2 = {2,2,2};
  21.  
  22. std::cout << dot(v1, v2) << '\n';
  23.  
  24. double p[3] = {1,2,3};
  25. double q[3] = {2,2,2};
  26.  
  27. std::cout << dot(p, q) << '\n';
  28. }
  29.  
Success #stdin #stdout 0s 2960KB
stdin
Standard input is empty
stdout
12
12