## accumulate
## Prototype
template <class InputIterator, class T> T accumulate(InputIterator first, InputIterator last, T init); template <class InputIterator, class T, class BinaryFunction> T accumulate(InputIterator first, InputIterator last, T init, BinaryFunction binary_op); ## Description
The functors ## DefinitionDefined in the standard header numeric, and in the nonstandard backward-compatibility header algo.h. ## Requirements on typesFor the first version, the one that takes two arguments: -
`InputIterator` is a model of InputIterator. -
`T` is a model of Assignable. -
If
`x` is an object of type`T` and`y` is an object of`InputIterator` 's value type, then`x + y` is defined. -
The return type of
`x + y` is convertible to`T` .
For the second version, the one that takes three arguments: -
`InputIterator` is a model of InputIterator. -
`T` is a model of Assignable. -
`BinaryFunction` is a model of BinaryFunction. -
`T` is convertible to`BinaryFunction` 's first argument type. -
The value type of
`InputIterator` is convertible to`BinaryFunction` 's second argument type. -
`BinaryFunction` 's return type is convertible to`T` .
## Preconditions-
`[first, last)` is a valid range.
## ComplexityLinear. Exactly ## Exampleint main() { int A[] = {1, 2, 3, 4, 5}; const int N = sizeof(A) / sizeof(int); cout << "The sum of all elements in A is " << accumulate(A, A + N, 0) << endl; cout << "The product of all elements in A is " << accumulate(A, A + N, 1, multiplies<int>()) << endl; } ## Notes[1] There are several reasons why it is important that ## See also |