使用 STL 在 C++ 中实现数组乘积

c++server side programmingprogramming更新于 2024/9/8 16:17:00

这是一个用于查找数组乘积的 C++ 程序示例。

算法

开始
    初始化数组的值。
    调用已定义的函数累积以返回数组的乘积。
    打印解决方案。
结束。

示例代码

#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

相关文章