A RetroSearch Logo

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

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_valarray_sum.htm below:

C++ valarray Library - Function sum

C++ valarray Library - Function sum Description

It 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.

Declaration

Following is the declaration for std::valarray::sum function.

T sum() const;
C++11
T sum() const;
Parameters

none

Return Value

It returns the sum of all the elements in the valarray.

Exceptions

Basic guarantee − if any operation performed on the elements throws an exception.

Data races

All elements effectively copied are accessed.

Example

In below example explains about std::valarray::sum function.

#include <iostream>
#include <valarray>

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

   return 0;
}

Let us compile and run the above program, this will produce the following result −

The sum is 60

valarray.htm


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