Skip to content

Latest commit

 

History

History
76 lines (42 loc) · 1.75 KB

CppExerciseNoForLoopsAnswer9.md

File metadata and controls

76 lines (42 loc) · 1.75 KB

 

 

 

 

 

 

This is the answer of Exercise #9: No for-loops.

 

 

 

 

 

Question #9: Product of std::vector<int>

 

Replace the for-loop. You will need:

 


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

 

 

 

 

 

Answer

 


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