
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
Sum Up Elements of a C++ Vector
In this article, we will understand how to sum the elements present inside a vector in C++. A vector is a dynamically allocated array with variable size. The sum of elements of a vector can be calculated in a number of ways, and we will discuss two such ways.
Problem Statement
Given a non-empty vector of integers, calculate the sum of all elements of the vector using multiple approaches in C++.
Examples
The following examples give the input and output of some test cases :
Input
vectorvec={1,3,4,5,2,3}
Output
18
Input
vectorvec={1,-1,-4,-5,2,3}
Output
-4
Input
vectorvec={0}
Output
0
Approaches to Find the Sum of Elements of a Vector
We will discuss two different methods to find the sum of elements of a vector. These approaches are as follows :
Using accumulate() Method
The first way is by using std::accumulate(), which is defined in <numeric> header file. It accumulates all the values present specified in the vector to the specified sum. The accumulate() function takes three parameters:
- Iterator to the first element of the given vector.
- Iterator to the last element of the given vector.
- Initial value of sum, which is 0 in our case.
Pseudocode
The following steps are required to calculate the sum of elements of a vector using accumulate() method :
- Begin
- Declare v of vector type.
- Initialize some values into v vector in array pattern.
- Print "Sum of all the elements is : ".
- Call accumulate(v.begin(),v.end(),0) to calculate the sum of all values of v vector.
- Print the result of sum.
- End
Example
The following code is used to calculate the sum of elements of a vector using accumulate() method.
#include<iostream> #include<vector> #include<numeric> using namespace std; int main() { vector<int> v = {2,7,6,10}; //vector v is given to us cout<<"Sum of all the elements is : "<<endl; cout<<accumulate(v.begin(),v.end(),0); //using accumulate method }
Output
Sum of all the elements is: 25
Time and Space Complexity
The time complexity of the above code is O(N), where N is the number of elements in the vector.
The space complexity of the above code is constant or O(1), as we take no extra space to store elements.
Using Iterative Method
We can also iterate over the whole vector, and calculate the sum. First, we have to declare a variable sum, which is initialized to 0. Then, we successively add each element of the vector to sum one by one as we iterate over the vector.
Pseudocode
The following steps are required to calculate the sum of elements of a vector using iterative method:
- Begin
- Declare v vector of int data type, and a variable sum of type int.
- Initialize sum=0 , and put some values into v vector as per the problem statement.
- Print "Sum of all the elements is : ".
- Iterate over the vector using the auto iterator.
- With each iteration, add the value of the iterator to the sum variable.
- Output the value of the sum variable.
- End
Example
The following code is used to calculate the sum of elements of a vector using iterative method.
#include<iostream> #include<vector> #include<numeric> using namespace std; int main() { vector<int> v = {2,7,6,10}; //vector v is given to us int sum=0; cout<<"Sum of all the elements is : "<<endl; for(auto& itr:v){ sum+=itr; } cout<<sum<<endl; }
Output
Sum of all the elements is: 25
Time and Space Complexity
The time complexity of the above code is O(N), where N is the number of elements in the vector.
The space complexity of the above code is constant or O(1), as we take no extra space to store elements.