STL 之accumulate,adjacent_difference,inner_product,partial_sum
生活随笔
收集整理的這篇文章主要介紹了
STL 之accumulate,adjacent_difference,inner_product,partial_sum
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
accumulate,adjacent_difference,inner_product,partial_sum 這些算法都是數字算法,因此只能操作數字類型的數據。
#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);?? #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;?? }??
頭文件
#include <numeric>
聲明:
inner_product : init = init op1 (elem op2 elem)
運行結果:
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STL 之map,multimap
- 下一篇: STL 之for_each,transf