Skip to content

Latest commit

 

History

History
39 lines (22 loc) · 2.2 KB

File metadata and controls

39 lines (22 loc) · 2.2 KB

 

 

 

 

 

 

Matrix code snippet to sum all values in a two-dimensional matrix. To sum all values in a one-dimensional container, go to the GetSum (on container) page.

 


#include <algorithm> #include <numeric> #include <vector> //From http://www.richelbilderbeek.nl/CppGetSum.htm template <class T> const T::value_type GetSum(const T& v) {   return std::accumulate(v.begin(), v.end(), static_cast<T::value_type>(0.0)); } //From http://www.richelbilderbeek.nl/CppGetSumMatrix.htm template <class T> const T GetSum(const std::vector<std::vector<T> >& v) {   T sum = static_cast<T>(0.0);   typedef std::vector<std::vector<T> >::const_iterator Iterator;   const Iterator j = v.end();   for (Iterator i = v.begin(); i!=j; ++i)   {     sum+=GetSum(*i);   }   return sum; }