Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

partial_sum


Generalized Numeric Operation

Summary

Calculates successive partial sums of a range of values.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

#include <numeric>
template <class InputIterator, class OutputIterator>
OutputIterator partial_sum (InputIterator first,
                            InputIterator last,
                            OutputIterator result);

template <class InputIterator,
          class OutputIterator,
          class BinaryOperation>
OutputIterator partial_sum (InputIterator first,
                            InputIterator last,
                            OutputIterator result,
                            BinaryOperation binary_op);

Description

The partial_sum algorithm creates a new sequence in which every element is formed by adding all the values of the previous elements, or, in the second form of the algorithm, by applying the operation binary_op successively on every previous element. That is, partial_sum assigns to every iterator i in the range [result, result + (last - first)) a value equal to:

or, in the second version of the algorithm:

For instance, applying partial_sum to (1,2,3,4,) yields (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

Program Output

Warnings

If your compiler does not support default template parameters, then you always need to include the Allocator template argument. For instance, you need to write:

vector<int, allocator<int> >

instead of:

vector<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998