last). [1]

The function object binary_op is not required to be either commutative or associative: the order of all of accumulate's operations is specified. The result is first initialized to init. Then, for each iterator i in [first, last), in order from beginning to end, it is updated by result = result + *i (in the first version) or result = binary_op(result, *i) (in the second version).

Definition

Defined in the standard header numeric, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

For the first version, the one that takes two arguments:

• InputIterator is a model of Input Iterator.

• 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 Input Iterator.

• T is a model of Assignable.

• BinaryFunction is a model of Binary Function.

• 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.

Complexity

Linear. Exactly last – first invocations of the binary operation.

Example

int 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 accumulate starts with the value init. One of the most basic is that this allows accumulate to have a well-defined result even if [first, last) is an empty range: if it is empty, the return value is init. If you want to find the sum of all of the elements in [first, last), you can just pass 0 as init.

See also

inner_product, partial_sum, adjacent_difference, count

inner_product

Category: algorithms

Component type: function

Prototype

Inner_product is an overloaded name; there are actually two inner_product functions.

template <class InputIterator1, class InputIterator2, class T>

T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);

template <class InputIterator1, class InputIterator2, class T, class BinaryFunction1, class BinaryFunction2>

T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryFunction1 binary_op1, BinaryFunction2 binary_op2);

Description

Inner_product calculates a generalized inner product of the ranges [first1, last1) and [first2, last2).

The first version of inner_product returns init plus the inner product of the two ranges [1]. That is, it first initializes the result to init and then, for each iterator i in [first1, last1) , in order from the beginning to the end of the range, updates the result by result = result + (*i) * *(first2 + (i – first1)) .

The second version of inner_product is identical to the first, except that it uses two user-supplied function objects instead of operator+ and operator*. That is, it first initializes the result to init and then, for each iterator i in [first1, last1), in order from the beginning to the end of the range, updates the result by result = binary_op1(result, binary_op2(*i, *(first2 + (i – first1))). [2]

Definition

Defined in the standard header numeric, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

For the first version:

• InputIterator1 is a model of Input Iterator.

• InputIterator2 is a model of Input Iterator.

• T is a model of Assignable.

• If x is an object of type T, y is an object of InputIterator1's value type, and z is an object of InputIterator2's value type, then x + y * z is defined.

• The type of x + y * z is convertible to T.

For the second version:

• InputIterator1 is a model of Input Iterator.

• InputIterator2 is a model of Input Iterator.

• T is a model of Assignable.

• BinaryFunction1 is a model of Binary Function.

• BinaryFunction2 is a model of Binary Function.

• InputIterator1's value type is convertible to

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату