A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/valarray/valarray/sum/ below:

public member function

<valarray>

std::valarray::sum

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 element and all the other elements, in an unspecified order.

If the valarray has a size of zero, it causes undefined behavior.

T shall support the calls to operator+=.



Parameters none

Return value The sum of all the elements in the valarray.
T is the template argument of valarray (the value type).

Example
1
2
3
4
5
6
7
8
9
10
11
12
// valarray::sum example
#include <iostream>     // std::cout
#include <valarray>     // std::valarray

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

  return 0;
}

Output:


Complexity Depends on library implementation (operations may be parallelized).

Iterator validity No changes: Valid iterators, references and sub-arrays keep their validity.

Data races Both the valarray and its elements are accessed.

Exception safety If applying operator+= to element values throws an exception, it causes undefined behavior.
If the valarray has a size of zero, it causes undefined behavior.

See also
valarray::min
Return smallest value (public member function)
valarray::max
Return greatest value (public member function)
valarray operators
Valarray operators (function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4