Namespaces
Variants
Views
Actions

std::accumulate

From cppreference.com
< cpp‎ | algorithm
Revision as of 11:03, 19 January 2013 by 76.126.114.29 (Talk)

 
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <numeric>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class InputIt, class T, class BinaryOperation >

T accumulate( InputIt first, InputIt last, T init,

              BinaryOperation op );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Computes the sum of the given value init and the elements in the range [first, last). The first version uses operator+ to sum up the elements, the second version uses the given binary function op.

Contents

Parameters

first, last - the range of elements to sum
init - initial value of the sum
op - binary operation function object that will be applied.

The signature of the function should be equivalent to the following:

 Ret fun(const Type1 &a, const Type2 &b);

The signature does not need to have const &.
The type Type1 must be such that an object of type T can be implicitly converted to Type1. The type Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type2. The type Ret must be such that an object of type T can be assigned a value of type Ret. ​

Type requirements
-
InputIt must meet the requirements of InputIterator.
-
T must meet the requirements of CopyAssignable and CopyConstructible.

Return value

The sum of the given value and elements in the given range.

Possible implementation

First version
template<class InputIt, class T>
T accumulate(InputIt first, InputIt last, T init)
{
    for (; first != last; ++first) {
        init = init + *first;
    }
    return init;
}
Second version
template<class InputIt, class T, class BinaryOperation>
T accumulate(InputIt first, InputIt last, T init, 
             BinaryOperation op)
{
    for (; first != last; ++first) {
        init = op(init, *first);
    }
    return init;
}

Example

#include <iostream>
#include <vector>
#include <numeric>
#include <string>
 
int multiply(int x, int y)
{
    return x*y;
}
 
std::string magic_function(std::string res, int x)
{
    return res += (x > 5) ? "b" : "s";
}
 
int main()
{
    std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
    int sum = std::accumulate(v.begin(), v.end(), 0);
    int product = std::accumulate(v.begin(), v.end(), 1, multiply);
    std::string magic = std::accumulate(v.begin(), v.end(), std::string(), 
                                        magic_function);
 
    std::cout << sum << '\n'
              << product << '\n'
              << magic << '\n';
}

Output:

55
3628800
sssssbbbbb

See also

Template:cpp/algorithm/dcl list adjacent differenceTemplate:cpp/algorithm/dcl list inner productTemplate:cpp/algorithm/dcl list partial sum