Standard C++ Library Class Reference

binary_op(binary_op(..., binary_op (*first, *(first + 1)),...),*(first + (i -
result)))
For instance, applying partial_sum to (1,2,3,4,) will yield (1,3,6,10).
The partial_sum algorithm returns result + (last - first).
If result is equal to first, the elements of the new sequence successively replace the elements in the original
sequence, effectively turning partial_sum into an inplace transformation.
Complexity
Exactly (last - first) - 1 applications of the default + operator or binary_op are performed.
Example
//
// partsum.cpp
//
#include <numeric> //for accumulate
#include <vector> //for vector
#include <functional> //for times
#include <iostream.h>
int main()
{
//Initialize a vector using an array of ints
int d1[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v(d1, d1+10);
//Create an empty vectors to store results
vector<int> sums((size_t)10), prods((size_t)10);
//Compute partial_sums and partial_products
partial_sum(v.begin(), v.end(), sums.begin());
partial_sum(v.begin(), v.end(), prods.begin(), times<int>());
//Output the results
cout << "For the series: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "The partial sums: " << endl << " " ;
copy(sums.begin(),sums.end(),
ostream_iterator<int>(cout," "));
cout <<" should each equal (N*N + N)/2" << endl << endl;
cout << "The partial products: " << endl << " ";
copy(prods.begin(),prods.end(),
ostream_iterator<int>(cout," "));
cout << " should each equal N!" << endl;
return 0;
}
Output :
For the series:
1 2 3 4 5 6 7 8 9 10
The partial sums:
1 3 6 10 15 21 28 36 45 55 should each equal (N*N + N)/2
The partial products:
1 2 6 24 120 720 5040 40320 362880 3628800 should each equal N!