STL系列------算法---------inner_product

 

 
如果要想真正学好STL 提供网站  http://www.cplusplus.com/reference/algorithm/count/

 

inner_product

<numeric>
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 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 parameters binary_op1 and binary_op2.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
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++))
               // for the binary_op's version
  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 argument, and returning the result of an accumulation operation. This can either be a pointer to a function or an object whose class overloads operator().
binary_op2
Binary operation taking two elements of type T as argument, and returning the result of the inner product operation. This can either be a pointer to a function or an object whose class overloads operator().

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


#include <iostream>
#include <functional>
#include <numeric>
using namespace std;

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};

  cout << "using default inner_product: ";
  cout << inner_product(series1,series1+3,series2,init);
  cout << endl;

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

  cout << "using custom functions: ";
  cout << inner_product(series1,series1+3,series2,init,myaccumulator,myproduct);
  cout << endl;

  return 0;
}

 

 

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

See also

你可能感兴趣的:(STL系列------算法---------inner_product)