
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to find the 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
- Related Questions & Answers
- How to find the sum of elements of an Array using STL in C++?
- How to find the maximum element of a Vector using STL in C++?
- How to sum up elements of a C++ vector?
- Sorting a vector of custom objects using C++ STL
- How to reverse a Vector using STL in C++?
- How to find the sum of all elements of a given matrix using Numpy?
- How to find the intersection of elements in a string vector in R?
- How to find the table of ordered frequencies of vector elements in R?
- C/C++ Program to find the sum of elements in a given array
- How to sort a Vector in descending order using STL in C++?
- How to find the sum of digits of a number using recursion in C#?
- How to find common elements between two Arrays using STL in C++?
- How to find common elements between two Vectors using STL in C++?
- How to find the maximum element of an Array using STL in C++?
- How to find the rank of a vector elements in R from largest to smallest?
Advertisements