inner_product例子

  1. // inner_product.cpp -- 2014-11-04-11.04  
  2. #include "stdafx.h"  
  3. #include <iostream>  
  4. #include <numeric>  
  5. #include <vector>  
  6. #include <functional>  
  7.   
  8. using std ::vector ;  
  9. using std ::plus ;  
  10. using std ::multiplies ;  
  11.   
  12. template<class T>  
  13. class Print  
  14. {  
  15. public:  
  16.     void operator () (const T & t) const  
  17.     {  
  18.         std ::cout << t << " " ;  
  19.     }  
  20. } ;  
  21.   
  22. int main(int argc, char* argv[])  
  23. {  
  24.     int arr1[] = {1, 2, 3, 4} ;  
  25.     vector<int> vec1(arr1, arr1 + sizeof arr1 / sizeof (int)) ;  
  26.     int arr2[] = {1, 1, 1, 2} ;  
  27.     vector<int> vec2(arr2, arr2 + sizeof arr2 / sizeof (int)) ;  
  28.   
  29.     //  inner_product (beg1, end1, beg2, init) ;  
  30.     //  操作前:[beg1,end1)和[beg2,...)标示输入序列.init是第一个参与运算的值.  
  31.     //  操作后:计算init依次加两个输入序列各自对应元素的乘积.  
  32.     //  返回值:计算所得的值.  
  33.     //  备注:     init是第一个参与运算的元素.init的类型决定返回值的类型.  
  34.     //                  [beg2,...)序列至少同[beg1,end1)序列一样大.否则将抛出异常.  
  35.     //                  [beg2,...)中超出[beg1,end1)长度的序列不参与运算.  
  36.     int totalValue = inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0) ;  
  37.     std ::cout << totalValue << std ::endl ;  
  38.   
  39.     //  inner_product (beg1, end1, beg2, init, plus<int> (), multiplies<int> ()) ;  
  40.     //  操作前:[beg1,end1)和[beg2,...)标示输入序列.init是第一个参与运算的值.plus<int> ()和multiplies<int> ()是二元函数对象.  
  41.     //  操作后:计算init依次加两个输入序列各自对应元素的乘积.  
  42.     //  返回值:计算所得的值.  
  43.     //  备注:     init是第一个参与运算的元素.init的类型决定返回值的类型.  
  44.     //                  [beg2,...)序列至少同[beg1,end1)序列一样大.否则将抛出异常.  
  45.     //                  [beg2,...)中超出[beg1,end1)长度的序列不参与运算.  
  46.     //                  plus<int> ()所在位置用来替换不带此参数版本算法的加.  
  47.     //                  multiplies<int> ()所在位置用来替换不带此参数版本算法的乘.  
  48.     totalValue = inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0, plus<int> (), multiplies<int> ()) ;  
  49.     std ::cout << totalValue << std ::endl ;  
  50.   
  51.     std ::cin.get() ;  
  52.  
      
  53.     std ::cout << std ::endl ;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值