# How to find the sum of elements of a vector in C++ STL?

**C++ STL | finding sum of the vector elements**: Here, we are going to learn **how to find the sum of the elements of a vector in C++ STL?**

Submitted by **IncludeHelp**, on May 18, 2019

Given a vector and we have to find the sum of the elements using C++ program.

## Finding sum of vector elements

To **find the sum of the elements**, we can use **accumulate() function** which is defined in **<numeric>** header in C++ standard template library. It accepts the range of the iterators in which we have to find the sum of the elements; it also accepts a third parameter which can be used to provide an initial value of the sum.

**Note:** To use vector – include <vector> header, and to use **sum() function** – include <numeric> header or we can simply use <bits/stdc++.h> header file.

**Syntax:**

sum(iterator start, iterator end, initial_sum_value);

Here, start_position, iterator end_position are the iterators pointing to the start and end elements in a container to be reversed.

initial_sum_value – is an initial value of the sum that will be added to the result of the sum of the elements.

**Example:**

Input: vector<int> v1{ 10, 20, 30, 40, 50}; sum(v1.begin(), v1.end(), 0); Output: 150

### C++ STL program to find the sum of the vector elements

//C++ STL program to find the sum of the vector elements #include <iostream> #include <numeric> #include <vector> using namespace std; int main() { //vector vector<int> v1{ 10, 20, 30, 40, 50 }; //printing elements cout << "vector elements..." << endl; for (int x : v1) cout << x << " "; cout << endl; //finding sum with initial value of sum is 0 int sum = accumulate(v1.begin(), v1.end(), 0); cout << "sum (with intial value of sum is 0): " << sum << endl; //finding sum with initial value of sum is 100 sum = accumulate(v1.begin(), v1.end(), 100); cout << "sum (with intial value of sum is 100): " << sum << endl; return 0; }

**Output**

vector elements... 10 20 30 40 50 sum (with intial value of sum is 0): 150 sum (with intial value of sum is 100): 250

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.