Array sum in C++ STL Last Updated : 14 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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 + 5 = 15Following are the different STL functions to find the array sum in C++:Table of ContentUsing accumulate()Using reduce() (C++ 17 Onwards)Using accumulate()In C++, STL provide the function std::accumulate() to find the sum of an array elements. It is defined inside the <numeric> header file.Syntaxstd::accumulate(first, last, sum);where first and last are the iterator to first element and the element just after the last element of the range.Example C++ // C++ program to find the array sum using // of std::accumulate() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {5, 10, 15, 11, 9}; int n = sizeof(arr) / sizeof(arr[0]); // Find array sum using std::accumulate() cout << accumulate(arr, arr + n, 0); return 0; } Output50Time Complexity: O(n), where n is the size of the array.Auxiliary Space: O(1) Using reduce() (C++ 17 Onwards)Since C++ 17 onwards, we can use the std::reduce() method to find the sum of all array elements. It is similar to std::accumulate() function and returns the sum of values of elements in the given range. It is also defined inside the <numeric> header file.Example C++ // C++ program to find the array sum using // of std::reduce() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {5, 10, 15, 11, 9}; int n = sizeof(arr) / sizeof(arr[0]); // Find array sum using std::reduce() cout << reduce(arr, arr + n, 0); return 0; } Output50Time Complexity: O(n), where n is the size of the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Array sum in C++ STL K kartik Improve Article Tags : Misc C++ Programs C++ DSA Arrays STL cpp-array CPP Examples +4 More Practice Tags : CPPArraysMiscSTL Similar Reads 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 Array Product in C++ Using STL 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 accumula 2 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 Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 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 What is the Max Array Length Limit in C++? In C++, arrays are data structures that store data of the same type in continuous memory locations. However, when working with arrays, it's important to be aware of certain limitations, including the maximum length of an array that can be declared. In this article, we will learn how we can find the 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 Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w 2 min read Creating array of pointers in C++ An array of pointers is an array of pointer variables. It is also known as pointer arrays. We will discuss how to create a 1D and 2D array of pointers dynamically. The word dynamic signifies that the memory is allocated during the runtime, and it allocates memory in Heap Section. In a Stack, memory 5 min read Like