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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值