accumulate() and partial_sum() in C++ STL

Last Updated : 20 Jul, 2026

The std::accumulate() and std::partial_sum() functions are numerical algorithms provided by the C++ Standard Library for performing cumulative operations on a range of elements. They are defined in the <numeric> header.

  • std::accumulate() computes a single accumulated result over a range.
  • std::partial_sum() computes cumulative results for every element in the range.
  • Both functions support custom binary operations in addition to the default addition.

std::accumulate()

The std::accumulate() function computes the accumulated value of all elements in a specified range. By default, it performs addition, but a custom binary operation can also be provided.

Syntax

std::accumulate(first, last, init, op);

Parameters

  • first: Iterator to the first element of the range.
  • last: Iterator to the element one after the last element of the range.
  • init: Initial value to start the accumulation with.
  • op (optional): Binary operation function or functor. Defaults to addition (+)

Return Value: Returns the accumulated value after performing the operation on each element.

C++
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main() {
    vector<int> vec = {5, 10, 15};

    // Compute sum of all elements
    int sum = accumulate(vec.begin(), vec.end(), 0);

    cout << sum << endl; 
    return 0;
}

Output
30

Explanation: This code calculates the sum of all elements in the vector by applying the default addition operation and returns a single accumulated value (30).

Example : Finding the Product of Vector Elements Using std::accumulate()

C++
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int multiply(int a, int b) {
    return a * b;
}

int main() {
    vector<int> vec = {5, 10, 15};

    // Compute product using custom function
    int product = accumulate(vec.begin(), vec.end(), 1, multiply);

    cout << product << endl; 
    return 0;
}

Output
750

Explanation: This code uses a custom function to multiply elements in the vector. accumulate() applies the multiplication operation cumulatively, producing a single product value (750).

std::partial_sum()

std::partial_sum() function assigns the cumulative sum of all the set of values lying in the given range and stores it in another container at corresponding positions. Just like accumulate() function, we can modify the partial_sum() function to perform any other binary operation.

Syntax

std::partial_sum (first, last, res, op);

Parameters

  • first: Iterator to the first element of the range
  • last: Iterator to the last elements of range.
  • res: iterator to the container (mostly vector) where partial sum will be stored.
  • op: An optional function pointer that provides the binary operation to perform. By default, addition is used to get the sum.

Return Value: Iterator to the imaginary element after the last element in the res container. (end iterator)

Example: Finding Cumulative Sum

C++
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main() {
    vector<int> vec = {5, 10, 15};
    vector<int> res(vec.size());

    // Compute cumulative sum
    partial_sum(vec.begin(), vec.end(), res.begin());

    for (int val : res)
        cout << val << " "; 
    return 0;
}

Output
5 15 30 

Explanation: This code computes the cumulative sum of vector elements and stores intermediate results in another container. Each position contains the sum of all elements up to that index (5 15 30).

Example: Finding Cumulative Product of Vector Elements std::partial_sum()

C++
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int multiply(int a, int b) {
    return a * b;
}

int main() {
    vector<int> vec = {5, 10, 15};
    vector<int> res(vec.size());

    // Compute cumulative product
    partial_sum(vec.begin(), vec.end(), res.begin(), multiply);

    for (int val : res)
        cout << val << " ";  
    return 0;
}

Output
5 50 750 

Explanation: This code uses a custom multiplication function with partial_sum() to calculate cumulative products. Each position stores the product of all previous elements, resulting in 5 50 750.

accumulate() Vs partial_sum()

Featurestd::accumulate()std::partial_sum()
OutputReturns a single accumulated valueStores cumulative values in a container
Result SizeOne valueSame size as input range
Default OperationAdditionAddition
Supports Custom OperationsYesYes
Typical UseSum, product, aggregate valuePrefix sums, cumulative products
Comment