Array product in C++ using STL



The product of an array refers to the multiplication result of all the elements in the array. The STL library of C++ provides several built-in functions to quickly calculate product of an array. In this article, we will explain all those STL functions with examples.

Consider the following input/output scenario to understand the concept of array product:

// Input array
int arr[] = {1, 2, 3, 4, 5};

// Output
120

Explanation: The product of the elements in the array is 1 * 2 * 3 * 4 * 5 = 120.

Following are the different STL functions to find the product of an array in C++:

Finding Array Product Using accumulate() Function

The accumulate function is used to accumulate the values in a range defined by two iterators. By default, it will calculate the sum of the elements in the range. However, you can also use it to find the product of elements by providing a custom comparator (i.e., multiplies<int>()) as a fourth argument. It is defined in the numeric header file.

Syntax

#include <numeric>

int res = accumulate(arr, arr + n, 1, multiplies<int>());

Where arr is the pointer to the first element of the array, n is the size of the array, and 1 is the initial value for multiplication.

Example

The code below uses the accumulate function to find the product of all elements in an array:

#include <iostream>
#include <numeric>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n  = sizeof(arr)/sizeof(arr[0]);
 
    // Finding product of all elements 
    int res = accumulate(arr, arr + n, 1, multiplies<int>());

    cout << res;
    return 0;
}

The output of the above code will be:

120

Finding Array Product Using reduce() Function

The reduce() function is another function from the STL library that reduces a range of elements to a single value. By default, it will reduce the elements in the range by adding them up to a single value. But, you can define a custom operation (i.e., multiplies<int>()) to find the product of elements using this function.

Similar to the accumulate() function, it is also defined in the numeric header file. The main difference here is that reduce() is suitable for large datasets and can be used with parallel execution policies.

Syntax

#include <numeric>

int res = reduce(arr, arr + n, 1, multiplies());

Now, let's see an example to understand how to use this function.

Example

The code below implements the reduce function to find the product of all elements in an array:

#include <iostream>
#include <numeric>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 2};
    int n  = sizeof(arr)/sizeof(arr[0]);
 
    // Finding product of all elements 
    int res = reduce(arr, arr + n, 1, multiplies<int>());

    cout << res;
    return 0;
}

The output of the above code will be:

12

Finding Array Product Using for_each() Function

The for_each function is another useful function from the STL library that can be used to iterate through the elements of an array and perform operations on them. It is defined in the algorithm header file. You can use it to find the product of all elements in an array by applying a lambda function that multiplies each element.

Syntax

#include <algorithm>

for_each(arr, arr + n, [](int &x) { x *= 2; });

Where arr is the pointer to the first element of the array, n is the size of the array, and the lambda function performs the multiplication operation on each element of the array. Now, let's see an example to understand how to use this function.

Example

The code below implements the for_each function to find the product of all elements in an array:

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n  = sizeof(arr)/sizeof(arr[0]);
 
    // Finding product of all elements 
    int product = 1;
    for_each(arr, arr + n, [&product](int x) { product *= x; });

    cout << product;
    return 0;
}

The output of the above code will be:

120

Conclusion

We have discussed three STL functions, accumulate(), reduce(), and for_each(), to find the product of elements in an array in C++. Each function has its own use case, and you can choose one based on your specific requirements. The accumulate function is straightforward approach for small arrays, the reduce function is best for large datasets due to its parallel execution capabilities, and the for_each function provides flexibility with lambda functions for custom operations.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-07-21T18:54:59+05:30

289 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements