W3cubDocs

/C++

std::accumulate

Defined in header <numeric>
(1)
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
(until C++20)
template< class InputIt, class T >
constexpr T accumulate( InputIt first, InputIt last, T init );
(since C++20)
(2)
template< class InputIt, class T, class BinaryOperation >
T accumulate( InputIt first, InputIt last, T init,
              BinaryOperation op );
(until C++20)
template< class InputIt, class T, class BinaryOperation >
constexpr T accumulate( InputIt first, InputIt last, T init,
                        BinaryOperation op );
(since C++20)

Computes the sum of the given value init and the elements in the range [firstlast).

1) Initializes the accumulator acc (of type T) with the initial value init and then modifies it with acc = acc + *i (until C++20)acc = std::move(acc) + *i (since C++20) for every iterator i in the range [firstlast) in order.
2) Initializes the accumulator acc (of type T) with the initial value init and then modifies it with acc = op(acc, *i) (until C++20)acc = op(std::move(acc), *i) (since C++20) for every iterator i in the range [firstlast) in order.
If op invalidates any iterators (including the end iterators) or modifies any elements of the range involved, the behavior is undefined.

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 LegacyInputIterator.
-T must meet the requirements of CopyAssignable and CopyConstructible.

Return value

acc after all modifications.

Possible implementation

accumulate (1)
template<class InputIt, class T>
constexpr // since C++20
T accumulate(InputIt first, InputIt last, T init)
{
    for (; first != last; ++first)
        init = std::move(init) + *first; // std::move since C++20
 
    return init;
}
accumulate (2)
template<class InputIt, class T, class BinaryOperation>
constexpr // since C++20
T accumulate(InputIt first, InputIt last, T init, BinaryOperation op)
{
    for (; first != last; ++first)
        init = op(std::move(init), *first); // std::move since C++20
 
    return init;
}

Notes

std::accumulate performs a left fold. In order to perform a right fold, one must reverse the order of the arguments to the binary operator, and use reverse iterators.

Common mistakes

If left to type inference, op operates on values of the same type as init which can result in unwanted casting of the iterator elements. For example, std::accumulate(v.begin(), v.end(), 0) likely does not give the result one wishes for when v is of type std::vector<double>.

Example

#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
 
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, std::multiplies<int>());
 
    auto dash_fold = [](std::string a, int b)
    {
        return std::move(a) + '-' + std::to_string(b);
    };
 
    std::string s = std::accumulate(std::next(v.begin()), v.end(),
                                    std::to_string(v[0]), // start with first element
                                    dash_fold);
 
    // Right fold using reverse iterators
    std::string rs = std::accumulate(std::next(v.rbegin()), v.rend(),
                                     std::to_string(v.back()), // start with last element
                                     dash_fold);
 
    std::cout << "sum: " << sum << '\n'
              << "product: " << product << '\n'
              << "dash-separated string: " << s << '\n'
              << "dash-separated string (right-folded): " << rs << '\n';
}

Output:

sum: 55
product: 3628800
dash-separated string: 1-2-3-4-5-6-7-8-9-10
dash-separated string (right-folded): 10-9-8-7-6-5-4-3-2-1

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 242 C++98 op could not have side effects it cannot modify the ranges involved
LWG 2055
(P0616R0)
C++20 acc was not moved while being accumulated it is moved

See also

computes the differences between adjacent elements in a range
(function template)
computes the inner product of two ranges of elements
(function template)
computes the partial sum of a range of elements
(function template)
(C++17)
similar to std::accumulate, except out of order
(function template)
(C++23)
left-folds a range of elements
(niebloid)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/accumulate