How to find the sum of elements of an Array using STL in C++? Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array arr[], find the sum of the elements of this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: 259 Input: arr[] = {1, 7, 5, 4, 6, 12} Output: 35 Approach: Sum can be found with the help of accumulate() function provided in STL. Syntax: accumulate(first_index, last_index, initial value of sum); Below is the implementation of the above approach: CPP // C++ program to find the sum // of Array using accumulate() in STL #include <bits/stdc++.h> using namespace std; int main() { // Get the array int arr[] = { 1, 45, 54, 71, 76, 12 }; // Compute the sizes int n = sizeof(arr) / sizeof(arr[0]); // Print the array cout << "Array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; // Find the sum cout << "\nSum = " << accumulate(arr, arr + n, 0); return 0; } Output: Array: 1 45 54 71 76 12 Sum = 259 Comment More infoAdvertise with us Next Article How to Find the Sum of All Elements in a Deque in C++? C code_r Follow Improve Article Tags : Misc C++ Programs C++ STL cpp-array +1 More Practice Tags : CPPMiscSTL Similar Reads How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read How to Find Sum of All Elements of a Vector using STL in C++? In this article, we will find the sum of all the elements of the vector using STL in C++.The simplest method to find the sum of all elements of vector using STL is accumulate() method. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector 2 min read How to Find Sum of All Elements of a Vector using STL in C++? In this article, we will find the sum of all the elements of the vector using STL in C++.The simplest method to find the sum of all elements of vector using STL is accumulate() method. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector 2 min read How to Find the Sum of All Elements in a List in C++ STL? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows to store data in non-contiguous memory locations. In this article, we will learn how to find the sum of all elements in a list in C++. Example: Input:myList = {10, 20, 30, 40, 50};Outpu 2 min read How to Find the Sum of All Elements in a Deque in C++? In C++, double-ended queues are sequence containers similar to vectors but are more efficient in the case of insertion and deletion of elements at both ends. In this article, we will learn how to find the sum of all elements in a deque in C++. Example Input: myDeque ={1,2,3,4,5} Output: Sum of all d 2 min read How to Find the Cumulative Sum of Array in C++? In C++, the cumulative sum, also known as the prefix sum of an array is the sum of all elements of the array at the current index including the sum of the previous elements. In this article, we will learn how to find the prefix sum of elements in an array in C++. Example Input: arr[]= {1,2,3,4,5} Ou 2 min read Like