Syntax:
#include <valarray> TYPE sum() const;
The sum() function returns the summation of all element values using the '+=' operator. If TYPE is not a built-in type, a '+=' operator must exist.
This function does not affect the contents of the valarray.
The following code will return the sum of values in the array:
#include <valarray> #include <iostream> const double vd[] = {1,2,3,4,5}; std::valarray<int> v1(vd,5); std::cout << v1.sum() << std::endl;
Will produce this output:
15