Sitemap

C++ accumulate() Function

2 min readNov 19, 2024

--

Computes the sum of the value of init and elements in the range [first, last).

Parameters

first, last — the range of elements to sum

init — initial value of the sum

Get Sriram Reddy’s stories in your inbox

Join Medium for free to get updates from this writer.

op-binary operation function object that will be applied.

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;
}

Example

Note: accumulate is left-fold

#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

Example — 2

// accumulate example
#include <iostream> // std::cout
#include <functional> // std::minus
#include <numeric> // std::accumulate

int myfunction (int x, int y) {return x+2*y;}
struct myclass {
int operator()(int x, int y) {return x+3*y;}
} myobject;

int main () {
int init = 100;
int numbers[] = {10,20,30};

std::cout << "using default accumulate: ";
std::cout << std::accumulate(numbers,numbers+3,init);
std::cout << '\n';

std::cout << "using functional's minus: ";
std::cout << std::accumulate (numbers, numbers+3, init, std::minus<int>());
std::cout << '\n';

std::cout << "using custom function: ";
std::cout << std::accumulate (numbers, numbers+3, init, myfunction);
std::cout << '\n';

std::cout << "using custom object: ";
std::cout << std::accumulate (numbers, numbers+3, init, myobject);
std::cout << '\n';

return 0;
}

Output:

using default accumulate: 160
using functionals minus: 40
using custom function: 220
using custom object: 280

--

--

Sriram Reddy
Sriram Reddy

Written by Sriram Reddy

Investing my energy to become best version of myself.