Skip to content

Latest commit

 

History

History
62 lines (35 loc) · 1.73 KB

CppProduct.md

File metadata and controls

62 lines (35 loc) · 1.73 KB

Math code snippet to calculate the product of the elements of a std::vector.

 

Product can be programmed multiple ways:

 

 

 

 

 

 


#include <vector> int Product(const std::vector<int>& v) {   const int sz = v.size();   const int product = 1;   for (int i=0; i!=sz; ++i)   {     product*=v[i];   }   return product; }

 

 

 

 

 

Product using a for-loop

 


#include <vector> #include <algorithm #include <numeric> int Product(const std::vector<int>& v) {   return std::accumulate(v.begin(),v.end(),1,std::multiplies<int>()); }