Книга: Standard Template Library Programmer
inner_product
Разделы на этой странице:
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 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
- Chapter 16. Commercial products based on Linux, iptables and netfilter
- Глава 1. Этаж Product
- Product placement на телевидении
- CHAPTER 6 Productivity Applications
- Productivity Applications Written for Microsoft Windows
- 7.6.1 Making It a Product
- Как мы работаем с product backlog’ом
- 3 глава Психологические приемы воздействия product placement на управление потребительским поведением
- История product placement
- The Inner Workings of uucico
- Синтаксис явного INNER JOIN в SQL-92
- Product placement в художественной литературе