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)