
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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?
- 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?
- Sorting a vector of custom objects using C++ STL
- How to sort a Vector in descending order using STL in C++?
- How to find the table of ordered frequencies of vector elements in R?
- How to find the rank of a vector elements in R from largest to smallest?
- How to find all combinations of a vector elements without space in R?
- 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 frequency vector elements that exists in another vector in R?
- How to find the sum of all elements of a given array in JavaScript?

Advertisements