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::at() in C++ STL K kartik Improve Article Tags : C++ STL cpp-array CPP Examples Practice Tags : CPPSTL Similar Reads STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar 5 min read array::at() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::at() This function is used to return the reference to the element present at the position given as the par 2 min read array::size() in C++ STL The array::size() method is used to find the number of elements in the array container. It is the member method std::array class defined inside <array> header file. In this article, we will learn about the array::size() method in C++.Example:C++// C++ Program to illustrate the use of array::si 2 min read Array of Vectors in C++ STL Prerequisite: Arrays in C++, Vector 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. Vectors are known as dynam 3 min read array::empty() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::empty() empty() function is used to check if the array container is empty or not. Syntax : arrayname.empty 1 min read Array of Strings in C++ In C++, a string is sequence of characters that is used to store textual information. Internally, it is implemented as a dynamic array of characters. Array of strings is the array in which each element is a string.We can easily create an array of string in C++ as shown in the below example:C++#inclu 4 min read Like