What/where are the practical uses of the partial_sum
algorithm in STL?
What are some other interesting/non-trivial examples or use-cases?
You could build a "moving sum" (precursor to a moving average):
template
void moving_sum (const vector& in, int num, vector& out)
{
// cummulative sum
partial_sum (in.begin(), in.end(), out.begin());
// shift and subtract
int j;
for (int i = out.size() - 1; i >= 0; i--) {
j = i - num;
if (j >= 0)
out[i] -= out[j];
}
}
And then call it with:
vector v(10);
// fill in v
vector v2 (v.size());
moving_sum (v, 3, v2);