public member function

std::valarray::sum

<valarray>
T sum() const;
Return sum of elements
Returns the sum of all the elements in the valarray as if calculated by applying operator+= to a copy of one enelement and all the other elements in an unspecified order.

The use of this member function requires that operator+= can be applied to T.

Parameters

none

Return value

The sum of all the elements in the valarray.
T is the template type of valarray (the elements' type).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// valarray::sum example
#include <iostream>
#include <valarray>
using namespace std;

int main ()
{
  int init[]={10,20,30,40};
  valarray<int> myvalarray (init,4);
  cout << "The sum is " << myvalarray.sum() << endl;

  return 0;
}


Output:

The sum is 100

See also