function template
<numeric>

std::inner_product

sum/multiply (1)
template <class InputIterator1, class InputIterator2, class T>   T inner_product (InputIterator1 first1, InputIterator1 last1,                    InputIterator2 first2, T init);
custom (2)
template <class InputIterator1, class InputIterator2, class T,          class BinaryOperation1, class BinaryOperation2>   T inner_product (InputIterator1 first1, InputIterator1 last1,                    InputIterator2 first2, T init,                    BinaryOperation1 binary_op1,                    BinaryOperation2 binary_op2);
Compute cumulative inner product of range
Returns the result of accumulating init with the inner products of the pairs formed by the elements of two ranges starting at first1 and first2.

The two default operations (to add up the result of multiplying the pairs) may be overridden by the arguments binary_op1 and binary_op2.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
template <class InputIterator1, class InputIterator2, class T>
   T inner_product (InputIterator1 first1, InputIterator1 last1,
                    InputIterator2 first2, T init)
{
  while (first1!=last1) {
    init = init + (*first1)*(*first2);
               // or: init = binary_op1 (init, binary_op2(*first1,*first2));
    ++first1; ++first2;
  }
  return init;
}

Parameters

first1, last1
Input iterators to the initial and final positions in the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
first2
Input iterator to the initial position in the second sequence. The range starts at first2 and has as many elements as the range above ([first1,last1)).
init
Initial value for the accumulator.
binary_op1
Binary operation taking two elements of type T as arguments, and returning the result of an accumulation operation.
This can either be a function pointer or a function object.
binary_op2
Binary operation taking two elements of type T as arguments, and returning the result of the inner product operation.
This can either be a function pointer or a function object.
Neither operations shall modify any of the elements passed as its arguments.

Return value

The result of accumulating init and the products of all the pairs of elements in the ranges starting at first1 and first2.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// inner_product example
#include <iostream>     // std::cout
#include <functional>   // std::minus, std::divides
#include <numeric>      // std::inner_product

int myaccumulator (int x, int y) {return x-y;}
int myproduct (int x, int y) {return x+y;}

int main () {
  int init = 100;
  int series1[] = {10,20,30};
  int series2[] = {1,2,3};

  std::cout << "using default inner_product: ";
  std::cout << std::inner_product(series1,series1+3,series2,init);
  std::cout << '\n';

  std::cout << "using functional operations: ";
  std::cout << std::inner_product(series1,series1+3,series2,init,
                                  std::minus<int>(),std::divides<int>());
  std::cout << '\n';

  std::cout << "using custom functions: ";
  std::cout << std::inner_product(series1,series1+3,series2,init,
                                  myaccumulator,myproduct);
  std::cout << '\n';

  return 0;
}

Output:

using default inner_product: 240
using functional operations: 70
using custom functions: 34


Complexity

Linear in the distance between first1 and last1.

Data races

The elements in both ranges are accessed (each element is accessed exactly once).

Exceptions

Throws if any of the operations on the elements or iterators throws.
Note that invalid arguments cause undefined behavior.

See also