Component type: function
Prototype
Inner_product is an overloaded name; there are actually two inner_product functions.
template
T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
template
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 BinaryFunction2 's first argument type.
• InputIterator2 's value type is convertible to BinaryFunction2 's second argument type.
• T is convertible to BinaryFunction1 's first argument type.
• BinaryFunction2 's return type is convertible to BinaryFunction1 's second argument type.
• BinaryFunction1 's return type is convertible to T .
Preconditions
• [first1, last1) is a valid range.
• [first2, first2 + (last1 – first1)) is a valid range.
Complexity
Linear. Exactly last1 – first1 applications of each binary operation.
Example
int main() {
int A1[] = {1, 2, 3};
int A2[] = {4, 1, –2};
const int N1 = sizeof(A1) / sizeof(int);
cout << "The inner product of A1 and A2 is " << inner_product(A1, A1 + N1, A2, 0) << endl;
}
Notes
[1] There are several reasons why it is important that inner_product starts with the value init . One of the most basic is that this allows inner_product to have a well-defined result even if [first1, last1) is an empty range: if it is empty, the return value is init . The ordinary inner product corresponds to setting init to 0.
[2] Neither binary operation is required to be either associative or commutative: the order of all operations is specified.
See also
accumulate , partial_sum , adjacent_difference , count
Category: algorithms
Component type: function
Prototype
Partial_sum is an overloaded name; there are actually two partial_sum functions.
template
OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result);
template
OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
Description
Partial_sum calculates a generalized partial sum: *first is assigned to *result , the sum of *first and *(first + 1) is assigned to *(result + 1) , and so on. [1]
More precisely, a running sum is first initialized to *first and assigned to *result . For each iterator i in [first + 1, last) , in order from beginning to end, the sum is updated by sum = sum + *i (in the first version) or sum = binary_op(sum, *i) (in the second version) and is assigned to *(result + (i – first)) . [2]
Definition
Defined in the standard header numeric, and in the nonstandard backward-compatibility header algo.h.
Requirements on types
For the first version:
• InputIterator is a model of Input Iterator.
• OutputIterator is a model of Output Iterator.
• If x and y are objects of InputIterator 's value type, then x + y is defined.
• The return type of x + y is convertible to InputIterator 's value type.
• InputIterator 's value type is convertible to a type in OutputIterator 's set of value types.
For the second version:
• InputIterator is a model of Input Iterator.
• OutputIterator is a model of Output Iterator.
• BinaryFunction is a model of BinaryFunction.
• InputIterator 's value type is convertible to BinaryFunction 's first argument type and second argument type.
• BinaryFunction 's result type is convertible to InputIterator 's value type.
• InputIterator 's value type is convertible to a type in OutputIterator 's set of value types.
Preconditions
• [first, last) is a valid range.
• [result, result + (last – first)) is a valid range.
Complexity
Linear. Zero applications of the binary operation if [first, last) is a empty range, otherwise exactly (last – first) – 1 applications.
Example
int main() {
const int N = 10;
int A[N];
Читать дальше