Generalized Operations
Prev | Chapter 12. Numerics | Next

## Generalized Operations

There are four generalized functions in the <numeric> header that follow the same conventions as those in <algorithm>. Each of them is overloaded: one signature for common default operations, and a second for fully general operations. Their names are self-explanatory to anyone who works with numerics on a regular basis:

- `accumulate`

- `inner_product`

- `partial_sum`

- `adjacent_difference`

Here is a simple example of the two forms of `accumulate`.

       int   ar[50];
       int   someval = somefunction();

       // ...initialize members of ar to something...

       int  sum       = std::accumulate(ar,ar+50,0);
       int  sum_stuff = std::accumulate(ar,ar+50,someval);
       int  product   = std::accumulate(ar,ar+50,1,std::multiplies<int>());

The first call adds all the members of the array, using zero as an initial value for `sum`. The second does the same, but uses `someval` as the starting value (thus, `sum_stuff == sum + someval`). The final call uses the second of the two signatures, and multiplies all the members of the array; here we must obviously use 1 as a starting value instead of 0.

The other three functions have similar dual-signature forms.

Prev | Up | Next
Chapter 12. Numerics | Home | Interacting with C