日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL 之accumulate,adjacent_difference,inner_product,partial_sum

發布時間:2024/4/11 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 之accumulate,adjacent_difference,inner_product,partial_sum 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
accumulate,adjacent_difference,inner_product,partial_sum 這些算法都是數字算法,因此只能操作數字類型的數據。

頭文件

#include <numeric>

聲明:

  • #include?<numeric>??
  • template?<class?inputItr,?class?Type>??
  • Type?accumulate(inputItr?first,?inputItr?last,?Type?init);??
  • template?<class?inputItr,?class?Type,?class?binaryOperation>??
  • Type?accumulate(inputItr?first,?inputItr?last,?Type?init,binaryOperation?op);??
  • ??
  • template<class?inputItr,?class?outputItr,?class?binaryOperation>??
  • outputItr?adjacent_difference(inputItr?first,inputItr?last,?outputItr?destFirst);??
  • template<class?inputItr,?class?outputItr>??
  • outputItr?adjacent_difference(inputItr?first,inputItr?last,?outputItr?destFirst,binaryOperation?op);??
  • ??
  • template<class?inputItr1,?class?inputItr2,class?Type>??
  • Type?inner_product(inputItr1?first1,inputItr1?last1,inputItr2?first2,inputItr2?last2,Type?init);??
  • template<class?inputItr1,?class?inputItr2,class?Type,?class?binaryOperation1,?class?binaryOperation2>??
  • Type?inner_product(inputItr1?first1,inputItr1?last1,inputItr2?first2,inputItr2?last2,Type?init,binaryOperation1?op1,binaryOperation2?op2);??
  • ??
  • template<class?inputItr,?class?outputItr>??
  • outputItr?partial_sum(inputItr?first,inputItr?last,outputItr?destFirst);??
  • template<class?inputItr,?class?outputItr,?class?binaryOperation>??
  • outputItr?partial_sum(inputItr?first,inputItr?last,outputItr?destFirst,binaryOperation?op);??

  • inner_product : init = init op1 (elem op2 elem)


    示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • #include?<string>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • #include?<algorithm>??
  • ??
  • #include?<numeric>??
  • ??
  • using?namespace?std;??
  • ??
  • void?print(vector<int>?vList)?{??
  • ????ostream_iterator<int>?screenOut(cout,"?");??
  • ????copy(vList.begin(),vList.end(),screenOut);??
  • ????cout?<<?endl;??
  • }??
  • ??
  • int?main()?{??
  • ????int?list[8]?=?{1,2,3,4,5,6,7,8};??
  • ????vector<int>?vecList(list,?list+8);??
  • ????vector<int>?newvList(8);??
  • ????cout?<<?"vecList:"?<<?endl;??
  • ????print(vecList);??
  • ??
  • ????int?sum?=?accumulate(vecList.begin(),vecList.end(),0);??
  • ????cout?<<?"sum:"?<<?sum?<<?endl;??
  • ??
  • ????int?product?=?accumulate(vecList.begin(),vecList.end(),1,multiplies<int>());??
  • ????cout?<<?"product:"?<<?product?<<?endl;??
  • ??
  • ????//?adjacent_difference??
  • ????adjacent_difference(vecList.begin(),vecList.end(),newvList.begin());??
  • ????cout?<<?"newvList:"?<<?endl;??
  • ????print(newvList);??
  • ??
  • ????adjacent_difference(vecList.begin(),vecList.end(),newvList.begin(),multiplies<int>());??
  • ????cout?<<?"newvList:"?<<?endl;??
  • ????print(newvList);??
  • ??
  • ????sum?=?inner_product(vecList.begin(),vecList.end(),newvList.begin(),0);??
  • ????cout?<<?"sum:"?<<?sum?<<?endl;??
  • ????cout?<<?"newvList:"?<<?endl;??
  • ????print(newvList);??
  • ??
  • ????sum?=?inner_product(vecList.begin(),vecList.end(),newvList.begin(),0,plus<int>(),minus<int>());??
  • ????cout?<<?"sum:"?<<?sum?<<?endl;??
  • ????cout?<<?"newvList:"?<<?endl;??
  • ????print(newvList);??
  • ??
  • ????partial_sum(vecList.begin(),vecList.end(),newvList.begin());??
  • ????cout?<<?"newvList:"?<<?endl;??
  • ????print(newvList);??
  • ??
  • ????partial_sum(vecList.begin(),vecList.end(),newvList.begin(),minus<int>());??
  • ????cout?<<?"newvList:"?<<?endl;??
  • ????print(newvList);??
  • ????return?0;??
  • }??

  • 運行結果:

    vecList:
    1 2 3 4 5 6 7 8
    sum:36
    product:40320
    newvList:
    1 1 1 1 1 1 1 1
    newvList:
    1 2 6 12 20 30 42 56
    sum:1093
    newvList:
    1 2 6 12 20 30 42 56
    sum:-133
    newvList:
    1 2 6 12 20 30 42 56
    newvList:
    1 3 6 10 15 21 28 36
    newvList:
    1 -1 -4 -8 -13 -19 -26 -34

    總結

    以上是生活随笔為你收集整理的STL 之accumulate,adjacent_difference,inner_product,partial_sum的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。