关于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;
}