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

vector vec={1,3,4,5,2,3}

Output

18

Input

vector vec={1,-1,-4,-5,2,3}

Output

-4

Input

vector vec={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 :

  1. Begin
  2. Declare v of vector type.
  3. Initialize some values into v vector in array pattern.
  4. Print "Sum of all the elements is : ".
  5. Call accumulate(v.begin(),v.end(),0) to calculate the sum of all values of v vector.
  6. Print the result of sum.
  7. 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:

  1. Begin
  2. Declare v vector of int data type, and a variable sum of type int.
  3. Initialize sum=0 , and put some values into v vector as per the problem statement.
  4. Print "Sum of all the elements is : ".
  5. Iterate over the vector using the auto iterator.
  6. With each iteration, add the value of the iterator to the sum variable.
  7. Output the value of the sum variable.
  8. 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.

Updated on: 2024-08-01T12:27:52+05:30

26K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements