STL 之accumulate,adjacent_difference,inner_product,partial_sum

38 篇文章 0 订阅

返回


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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yours风之恋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值