Standard C++ Library Class Reference

Description
There are two versions of inner_product. The first computes an inner product using the default
multiplication and addition operators, while the second allows you to specify binary operations
to use in place of the default operations.
The first version of the function computes its result by initializing the accumulator acc with the
initial value init and then modifying it with:
acc = acc + ((*i1) * (*i2))
for every iterator i1 in the range [first1, last1) and iterator i2 in the range [first2, first2 + (last1 -
first1)). The algorithm returns acc.
The second version of the function initializes acc with init, then computes the result:
acc = binary_op1(acc, binary_op2(*i1, *i2))
for every iterator i1 in the range [first1, last1) and iterator i2 in the range [first2, first2 + (last1 -
first1)).
Complexity
The inner_product algorithm computes exactly (last1 - first1) applications of either:
acc + (*i1) * (*i2)
or
binary_op1(acc, binary_op2(*i1, *i2)).
Example
//
// inr_prod.cpp
//
#include <numeric> //For inner_product
#include <list> //For list
#include <vector> //For vectors
#include <functional> //For plus and minus
#include <iostream.h>
int main()
{
//Initialize a list and an int using arrays of ints
int a1[3] = {6, -3, -2};
int a2[3] = {-2, -3, -2};
list<int> l(a1, a1+3);
vector<int> v(a2, a2+3);
//Calculate the inner product of the two sets of values