In C++, std::accumulate() walks through a range and folds it into one value. By default it adds every element to a running total, so it is the quickest way to get the sum of a std::vector.
You give it a start value, called init. It computes init + a1, then adds a2 to that, and so on, always from left to right. You can pass your own operation, such as std::multiplies or a lambda, to build a product, a joined string, or a maximum.
This function is declared in the <numeric> header. It exists since C++98, so every compiler supports it. It is a free function, called as std::accumulate(...), not with a dot.
Syntax
Give the range and a start value. A custom operation is optional.
C++
T sum1 = std::accumulate(first, last, init);
T sum2 = std::accumulate(first, last, init, op);
int total = std::accumulate(v.begin(), v.end(), 0);
Call
What it does
accumulate(first, last, init)
Returns init plus every element in the range, added in order from left to right.
accumulate(first, last, init, op)
Same walk, but each step is acc = op(acc, element) instead of acc + element.
The result has the type of init, not the type of the elements. 0 means int and 0.0 means double. This is the most common beginner mistake with this function.
Library declarations
C++
template<class InputIt, class T>
T accumulate(InputIt first, InputIt last, T init);
template<class InputIt, class T, class BinaryOp>
T accumulate(InputIt first, InputIt last, T init, BinaryOp op);
Parameters
Parameter
Description
first, last
Required. Iterators that mark the range [first, last). The element at last is not used.
init
Required. The start value of the total. Its type is also the type of the result.
op
Optional. A function or lambda that takes the running value and the next element and returns the new running value. The default is +.
Return Value
Return Value
Description
Value of type T
The final folded value. For an empty range it is simply init.
C++ Examples
Example 1 — Sum of a std::vector
The simplest use: add up every number in a vector, starting from 0.
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::accumulate(v.begin(), v.end(), 0);
std::cout << "sum = " << sum << '\n';
return 0;
}
Output
sum = 15
How It Works
The vector holds five numbers, and v.begin() and v.end() mark the whole range.
The start value is 0. accumulate() computes 0+1, then 1+2, then 3+3, then 6+4, then 10+5.
The last running total, 15, is returned and stored in sum.
Example 2 — Why the Type of init Matters
The result type comes from init. An int start value throws away the decimal part at every step.
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<double> v = {1.5, 2.5, 3.0};
auto wrong = std::accumulate(v.begin(), v.end(), 0); // int
auto right = std::accumulate(v.begin(), v.end(), 0.0); // double
std::cout << "int init: " << wrong << '\n';
std::cout << "double init: " << right << '\n';
return 0;
}
Output
int init: 6
double init: 7
How It Works
The vector holds double values: 1.5, 2.5 and 3.0. The true sum is 7.
With init = 0 the running total is an int. Each time a double is added, the answer is cut back to an int: 0+1.5 becomes 1, then 1+2.5 becomes 3, then 3+3.0 is 6.
With init = 0.0 the total stays a double all the way, so nothing is lost and the answer is 7. Always write 0.0 for floating-point data.
Example 3 — Custom Operations: Product and Joined Text
Pass std::multiplies for a product, or a lambda to join strings.
For the product, the start value is 1, not 0, because 0 times anything is 0. std::multiplies<int>() replaces the default plus.
For the sentence, the start value is the first word "C++", and the range begins at the second word. This avoids a leading space.
The lambda receives the text built so far and the next word, and returns acc + " " + w. After three words you get C++ is fun.
Frequently Asked Questions
std::accumulate() from <numeric> folds a range into one value. By default it adds all elements to a start value, from left to right. With a custom operation it can build a product, a joined string, a maximum, or any other combined result.
The result type is the type of the init argument. If you write 0, the running total is an int and every decimal part is cut off. Write 0.0 so the total stays a double.
accumulate() always goes from left to right, so any operation works. reduce() (C++17) may reorder the work and run in parallel, so the operation must be associative and commutative.
Call std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()). The start value must be 1, because starting from 0 would make every product 0.
No. It only reads the elements and returns a new value.