How to find the sum of elements of an Array using STL in C++? Last Updated : 19 Mar, 2019 Comments Improve Suggest changes 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 elements of an Array using STL 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 Mode in a Sorted Array in C++? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp 3 min read How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read How to Find the Mode of Numbers in an Array in C++? Mode of any dataset is the item that occurs most frequently in it. In this article, we will find the mode of numbers in an unsorted array in C++. For Example,Input: myArray = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 } Output: Mode : 2Finding Mode of Array Elements in C++To find the mode of the array elemen 2 min read How to Find the Size of a Set in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how we can find the size of a set container in C++. Example: Input: mySet={1 2 min read How to Find Largest Number in an Array in C++? In C++, arrays are used to store the collection of similar elements to be stored in adjacent memory locations. They can store data of any type such as int, char, float, etc. In this article, we will learn how to find the largest number in an array in C++. For Example,Input: myVector = {1, 3, 10, 7, 2 min read How to Find the Median of Vector Elements in C++? Median of a dataset is defined as the middle element when N(size of vector) is odd and the average of the middle two elements when N is even in the sorted dataset. In this article, we will learn how to find the median of all the elements of a vector in C++. For Example Input:myVector = {3, 1, 6, 2, 2 min read How to Find the Median of 2D Array in C++? The median can be defined as the middle element of a sorted array in the case of an odd number of elements in an array and the average of the middle two elements when the number of elements in an array is even. In this article, we will see how to calculate the median of a 2D array in C++. Example: I 3 min read How to Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int 2 min read How to Find First Occurrence of an Element in a Set in C++? In C++, a set is an ordered container that stores its unique values in some given order. In this article, we will see how to find the first occurrence of a specific element in a set in C++ STL. For Example, Input: mySet = {1, 2, 3, 8, 9, 11} Target = 9 Output: Element found at Index: 4Find the First 2 min read How to Find the Variance of Numbers in 2D Array in C++? The variance of numbers is a measure of the spread of a set of values which is used to find how much the values in a dataset differ from mean or average. In this article, we will learn how to find the variance of numbers in a 2D array in C++. Example Input: myMatrix = { { 10, 20, 30 }, { 40, 50, 60 3 min read Like