STL 之accumulate,adjacent_difference,inner_product,partial_sum
2015-05-28 16:19
363 查看
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
头文件
#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
- 【待完善】inner_product/adjacent_difference/partial_sum的用法
- C++ STL 算法库学习之adjacent_difference,count,partial_sum,accumulate,count_if详解
- STL-------------partial_sum
- STL-----------------adjacent_difference
- std::transform || inner_product || accumulate
- STL之inner_product
- (scan):STL-------------partial_sum
- STL之partial_sum
- STL之adjacent_difference
- stl中的adjacent_difference
- C++ STL inner_product
- STL系列------算法---------inner_product
- 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
- 【Smart OJ P2485】Sum of product 简单数学
- 学习STL算法:adjacent_find
- Project Euler 6 Sum square difference
- STL-----------accumulate
- CAFFE源码学习笔记之内积层-inner_product_layer