Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GetSum of a matrix

 

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;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict