 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the sum of elements of an Array using STL in C++?
Here we will see how to find the sum of all element of an array. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then sum will be: 405. So here we have to use the accumulate() function to solve this problem. This function description is present inside <numeric> header file.
Example
#include<iostream> #include<numeric> using namespace std; int main() {    int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27};    int n = sizeof(arr) / sizeof(arr[0]);    cout << "Array is like: ";    for (int i = 0; i < n; i++)    cout << arr[i] << " ";    cout << "\nSum of all elements: " << accumulate(arr, arr + n, 0); }  Output
Array is like: 12 45 74 32 66 96 21 32 27 Sum of all elements: 405
Advertisements
 