Skip to content

Latest commit

 

History

History
67 lines (37 loc) · 1.62 KB

CppExerciseNoForLoopsAnswer8.md

File metadata and controls

67 lines (37 loc) · 1.62 KB

 

 

 

 

 

 

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

 

Question #8: GetSum

 

Replace the for-loop. You will need:

 


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

 

 

 

 

 

Answer

 


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