
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Find Sum of Elements of a Vector Using STL in C++
In this tutorial, we will be discussing a program to understand how to find the sum of elements of a vector using STL in C++.
To find the sum of elements of a given vector, we would be using the accumulate() method from the STL library.
Example
#include <bits/stdc++.h> using namespace std; int main(){ //defining the vector vector<int> a = { 1, 45, 54, 71, 76, 12 }; cout << "Vector: "; for (int i = 0; i < a.size(); i++) cout << a[i] << " "; cout << endl; //calculating sum of the elements cout << "Sum = "<< accumulate(a.begin(), a.end(), 0); return 0; }
Output
Vector: 1 45 54 71 76 12 Sum = 259
Advertisements