当前位置:   article > 正文

关于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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

从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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/818529
推荐阅读
相关标签
  

闽ICP备14008679号