![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 sum up elements of a C++ vector?
Sum up of all elements of a C++ vector can be very easily done by std::accumulate method. It is defined in <numeric> header. It accumulates all the values present specified in the vector to the specified sum.
Algorithm
Begin Declare v of vector type. Initialize some values into v vector in array pattern. Print “Sum of all the elements are:”. Call accumulate(v.begin(),v.end(),0) to calculate the sum of all values of v vector. Print the result of sum. End.
Example Code
#include<iostream> #include<vector> #include<numeric> using namespace std; int main() { vector<int> v = {2,7,6,10}; cout<<"Sum of all the elements are:"<<endl; cout<<accumulate(v.begin(),v.end(),0); }
Output
Sum of all the elements are: 25
Advertisements