Array Product in C++ Using STL Last Updated : 29 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The product of all elements is the result of multiplying all the elements within the specified range. In this article we will learn different methods to find the product of all elements of an array in C++ using STL.The simplest method to find the product of all elements in array is by using accumulate() method. Let’s take a look at an example: C++ #include <bits/stdc++.h> 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 = accumulate(arr, arr + n, 1, multiplies<int>()); cout << res; return 0; } Output12Explanation: By default, the accumulate() method is used to find the sum of all elements but by providing the function object multiplies gives you the product of two numbers. We use this functor with accumulate function to find the product of all elements of vector.There are also some other methods in C++ by which we can calculate the product of all elements using STL. Some of them are as follows:Using for_each()The for_each() algorithm can also be used to find the product of all elements in an array by providing the lambda function in which we multiple every element of an array with variable storing the product of previous elements. C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); int res = 1; // Find product of all elements for_each(arr, arr + n, [&res](int i) { res *= i; }); cout << res; return 0; } Output12Using reduce() (C++ 17 Onwards)The reduce() method can also be used to find the product of all elements of an array by providing the function object multiplies just like accumulate() method. C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); // Find product of all elements int res = reduce(arr, arr + n, 1, multiplies<int>()); cout << res; return 0; } Output12 Comment More infoAdvertise with us Next Article Array sum in C++ STL K kartik Follow Improve Article Tags : Misc C++ Programs C++ cpp-array Practice Tags : CPPMisc Similar Reads Array sum in C++ STL Array sum can be defined as the sum of all elements of an array. In this article, we will learn how to find the array sum using C++ STL.ExamplesInput: arr[] = {5, 10, 15, 11, 9}Output: 50Explanation: As 5 + 10 + 15 + 11 + 19 = 50Input: arr[] = {1, 2, 3, 4, 5}Output: 15Explanation: As 1 + 2 + 3 + 4 + 2 min read Array of Sets in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Sets are a type of associative container in which each element has to be 5 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read Array of Pointers to Strings in C++ In C++, an array is a homogeneous collection of data that is stored in a contiguous memory location. We can store almost all types of data as array elements. In this article, we will learn how to store the array of pointers to strings in C++. Array of Pointers to Strings in C++A pointer to a string 6 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Resize an Array of Strings in C++? In C++, the array of strings is useful for storing many strings in the same container. Sometimes, we need to change the size of this array. In this article, we will look at how to resize the array of strings in C++. Resize String Array in C++There is no way to directly resize the previously allocate 2 min read Like