使用 STL 在 C++ 中进行数组相乘


这是一个用 C++ 编程找出数组乘积的示例。

算法

Begin
   Initialize the values of array.
   Call used defined function accumulate to return the product of array.
   Print the solution.
End.

示例代码

 现场演示

#include <iostream>
#include <numeric>
using namespace std;
int ProductOfArray(int p[], int n) {
   return accumulate(p, p + n, 1, multiplies<int>());
}
int main() {
   int m[] = {6,7 };
   int n = sizeof(m) / sizeof(m[0]);
   cout <<"Product of the Array is:" <<ProductOfArray(m, n);
}

输出

Product of the Array is:42

更新于:2019 年 7 月 30 日

185 次浏览

启动你的 事业

通过完成课程获得认证

开始
广告
© . All rights reserved.