Name

inner_product function template — Computes inner product of two ranges

Synopsis

template <typename InIter1, typename InIter2, typename T>
T inner_product(InIter1 first1, InIter1 last1, InIter2 first2, T init);
template <typename InIter1, typename InIter2, typename T, 
          typename BinaryOp1, typename BinaryOp2>
T inner_product(InIter1 first1, InIter1 last1, InIter2 first2, T init,
                BinaryOp1 binary_op1, BinaryOp2 binary_op2);

The inner_product function template computes an inner product of two ranges. It accumulates the products of corresponding items in [first1, last1) and [first2, last2), in which last2 = first2 + (last1 - first1). The second version calls binary_op1 as the accumulator operator (instead of addition) and binary_op2 as the multiplication operator.

Technical Notes

The result is computed as follows: for each i in the range [first1, last1), and for each j in [first2, last2), in which last2 = first2 + (last1 - first1), assign tmp = binary_op1(tmp, binary_op2(*i, *j)), in which tmp is initialized to init. The final value of tmp is returned.

The binary_op1 and binary_op2 functions or functors must not have side effects.

Complexity is linear: binary_op1 and binary_op2 are called exactly last - first times.

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.