accumulate() and partial_sum() functions are used to find the sum or any other accumulated value that is obtained by doing the addition or any other binary operation on the elements in the given range. Both of these functions are the part of STL Numeric Library and defined inside <numeric> header.
std::accumulate()
The std::accumulate() function is used to find the sum (or any other binary operation) of all the values lying in a range.
#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).
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.
Example : Finding the Product of Vector Elements Using std::accumulate()
#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.
#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).
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 Product of Vector Elements std::partial_sum()
#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.