In C++, std::partial_sum() calculates the prefix sums of a range, also called running totals or cumulative sums. The result at position i is the sum of all input elements from the start up to and including position i.
For the input 1 2 3 4 the output is 1 3 6 10. It is an inclusive scan, because every output includes its own element. You can write to another container, or back into the same one.
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::partial_sum(...), not with a dot.
Syntax
Give the input range and where to write. A custom operation is optional.
C++
auto end1 = std::partial_sum(first, last, d_first);
auto end2 = std::partial_sum(first, last, d_first, op);
std::partial_sum(v.begin(), v.end(), v.begin()); // in place
Call
What it does
partial_sum(first, last, d_first)
Writes the running sums of the input range to the output that starts at d_first.
partial_sum(first, last, d_first, op)
Same, but uses op instead of +. With std::multiplies you get running products.
The output may start at the same place as the input, so partial_sum(v.begin(), v.end(), v.begin()) changes the vector in place. Use std::back_inserter to write into an empty vector.
Library declarations
C++
template<class InputIt, class OutputIt>
OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first);
template<class InputIt, class OutputIt, class BinaryOp>
OutputIt partial_sum(InputIt first, InputIt last,
OutputIt d_first, BinaryOp op);
Parameters
Parameter
Description
first, last
Required. The input range [first, last).
d_first
Required. The start of the output. It needs room for as many elements as the input, or must be an inserter such as std::back_inserter.
op
Optional. A binary operation used instead of +.
Return Value
Return Value
Description
Output iterator
An iterator one past the last element written. For an empty input it is d_first.
C++ Examples
Example 1 — Running Totals into a New Vector
Write the prefix sums into an empty vector with std::back_inserter.
C++
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> out;
std::partial_sum(v.begin(), v.end(), std::back_inserter(out));
for (int n : out) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 3 6 10 15
How It Works
out starts empty. std::back_inserter(out) makes every written value call push_back(), so the vector grows by itself.
The first output is just 1. The second is 1+2 = 3, the third is 3+3 = 6, and the fourth is 6+4 = 10.
The last output, 15, is the sum of the whole vector. The loop prints all five running totals.
Example 2 — Prefix Sums in the Same Vector
Use the input start as the output start to replace the values with their running totals.
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {2, 4, 6, 8};
std::partial_sum(v.begin(), v.end(), v.begin());
for (int n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
2 6 12 20
How It Works
The output start is v.begin(), the same as the input start. This is allowed for partial_sum().
Each element is read before its place is overwritten, so the sums are correct: 2, then 2+4 = 6, then 6+6 = 12, then 12+8 = 20.
The original numbers are gone. Copy the vector first if you still need them, or use another output vector.
Example 3 — Running Product with std::multiplies
A different operation gives a different kind of running result. Here it builds factorials.
C++
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> out(v.size());
std::partial_sum(v.begin(), v.end(), out.begin(),
std::multiplies<int>());
for (int n : out) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 6 24 120
How It Works
out is created with five slots, because partial_sum() writes through out.begin() and does not grow the vector.
The fourth argument std::multiplies<int>() replaces +. Now each output is the product of all inputs so far.
The outputs are 1, 1*2 = 2, 2*3 = 6, 6*4 = 24, and 24*5 = 120. These are the factorials from 1! to 5!.
Frequently Asked Questions
It writes the running totals (prefix sums) of a range. Output number i is the sum of the first i+1 input elements. For 1 2 3 4 the output is 1 3 6 10.
Yes. Pass v.begin() as the output start. Each value is read before it is overwritten, so the running totals are correct. The original values are lost afterwards.
They give the same running totals. inclusive_scan() is the C++17 version: it may reorder the work, can run in parallel, and has an overload with an initial value. partial_sum() always works strictly left to right.
Yes. The first output is a copy of the first input, because there is nothing before it to add. With a custom operation the first output is still the first element.
The output range is probably too small. Create the output vector with the same size as the input, for example std::vector<int> out(v.size()), or use std::back_inserter on an empty vector.