首页 > 其他 > 详细

inner_product

时间:2019-02-16 20:25:27      阅读:181      评论:0      收藏:0      [点我收藏+]

版本一

 /**
   *  @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;
}

 

inner_product

原文:https://www.cnblogs.com/tianzeng/p/10388958.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!