其他分享
首页 > 其他分享> > inner_product

inner_product

作者:互联网

版本一

 /**
   *  @brief  Compute inner product of two ranges.
   *
   *  Starting with an initial value of @p __init, multiplies successive
   *  elements from the two ranges and adds each product into the accumulated
   *  value using operator+().  The values in the ranges are processed in
   *  order.
   *
   *  @param  __first1  Start of range 1.
   *  @param  __last1  End of range 1.
   *  @param  __first2  Start of range 2.
   *  @param  __init  Starting value to add other values to.
   *  @return  The final inner product.
   */
  template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
    inline _Tp
    inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
          _InputIterator2 __first2, _Tp __init)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
      __glibcxx_requires_valid_range(__first1, __last1);

      for (; __first1 != __last1; ++__first1, ++__first2)
    __init = __init + (*__first1 * *__first2);
      return __init;
    }

版本二

  /**
   *  @brief  Compute inner product of two ranges.
   *
   *  Starting with an initial value of @p __init, applies @p __binary_op2 to
   *  successive elements from the two ranges and accumulates each result into
   *  the accumulated value using @p __binary_op1.  The values in the ranges are
   *  processed in order.
   *
   *  @param  __first1  Start of range 1.
   *  @param  __last1  End of range 1.
   *  @param  __first2  Start of range 2.
   *  @param  __init  Starting value to add other values to.
   *  @param  __binary_op1  Function object to accumulate with.
   *  @param  __binary_op2  Function object to apply to pairs of input values.
   *  @return  The final inner product.
   */
  template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
       typename _BinaryOperation1, typename _BinaryOperation2>
    inline _Tp
    inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
          _InputIterator2 __first2, _Tp __init,
          _BinaryOperation1 __binary_op1,
          _BinaryOperation2 __binary_op2)
    {
      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
      __glibcxx_requires_valid_range(__first1, __last1);

      for (; __first1 != __last1; ++__first1, ++__first2)
    __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
      return __init;
    }
  1. 计算[first1,last1)和[first2,last2+(last1-first1))的一般化内积,如果要计算两个vector的一般化内积,把init置为0
  2. 第一个版本先执行result=init,然后执行result=result+(*i)*(first2+(i-first1))
  3. 第二个版本先执行result=init,再执行binary_op2(*i,*(first2+(i-first1)),然后执行binary_op1(result,binary_op2(*i,*(first2+(i-first1))),依次循环...
#include <iostream>
#include <numeric>
#include <vector>
#include <functional>
using namespace std;

class F1
{
    public:
        int operator()(int i,int j)
        {
            //clog<<" 执行F1"<<endl; 
            return i+j;
        }
};
class F2
{
    public:
        int operator()(int i,int j)
        {
            //clog<<" 执行F2"<<endl;
            return i/j;
        }
};
int main()
{
    vector<int> v{1,2,3};
    vector<int> v1{1,2,3,4,5,6};

    int t=inner_product(v.begin(),v.end(),v1.begin(),0);
    cout<<"t:"<<t<<endl;
    
    int t1=inner_product(v.begin(),v.end(),v1.begin(),0,F1(),F2());
    cout<<"t1:"<<t1<<endl;
    return 0;
}

 

标签:__,binary,product,param,init,inner,first1,first2
来源: https://www.cnblogs.com/tianzeng/p/10388958.html