关于accumulate的用法(累加和累乘)
关于accumulate的用法(累加和累乘)
从1到10累加
#include <iostream>
#include <numeric>
using namespace std;
template<class T>
T product(T a[], int n)
{
T theProduct = 0;
return accumulate(a, a+n, theProduct);
}
//multiplies<T>()
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
cout << product(a, 10) << endl;
return 0;
}
从1到10累乘
#include <iostream>
#include <numeric>
using namespace std;
template<class T>
T product(T a[], int n)
{
T theProduct = 1;
return accumulate(a, a+n, theProduct, multiplies<T>());//主要修改点在这里
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
cout << product(a, 10) << endl;
return 0;
}
【华为OD机试真题】可以转到CSDN相关专栏订阅学习:https://blog.csdn.net/weixin_45541762/article/details/129903356

浙公网安备 33010602011771号