Select Page

How to Sum the Elements of a Vector in C++

by | C++, Programming, Tips

The simplest way to sum the elements of a vector is to use the accumulate function. This tutorial will describe the different ways to sum the elements of a vector with code examples.


Sum Elements of a Vector Using accumulate

We can use the built-in accumulate() function from the numeric library. The function accumulates all the values present in the specified range. The function takes in a beginning iterator, an end iterator, and an initial value. Let’s look at an example of summing the elements of a vector of integers.

#include <iostream>
#include <vector>
#include <numeric>

int main() {

    // Vector of int

    std::vector<int> vec{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

    // Call accumulate function

    int sum = std::accumulate(vec.begin(), vec.end(), 0.0);

    // Print elements and sum

    std::cout<< "Vector elements: ";

    for (auto i : vec)

        std::cout<<i<<" ";

    std::cout << std::endl;

    std::cout << "Sum of elements in vector:  " << sum << std::endl;

    return 0;

}

Lets’ run the code to get the result:

Vector elements: 0 1 1 2 3 5 8 13 21 34 55 
Sum of elements in vector:  143

Sum Elements of a Vector Using for loop

We can use a for loop and the compound assignment operator to sum the values in a vector. Let’s look at an example of summing the elements of a vector of integers.

#include <iostream>
#include <vector>

int main() {

    // Vector of int

    std::vector<int> vec{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

    
    int sum = 0;
    // Sum elements using a for loop

    for (int i=0; i<vec.size(); i++)

        sum += vec[i];

    // Print elements and sum

    std::cout<< "Vector elements: ";

    for (auto i : vec)

        std::cout<<i<<" ";

    std::cout << std::endl;

    std::cout << "Sum of elements in vector:  " << sum << std::endl;

    return 0;

}

Let’s run the code to get the result:

Vector elements: 0 1 1 2 3 5 8 13 21 34 55 
Sum of elements in vector:  143

Sum Elements of a Vector Using Range-based for loop

The range-based for loop was introduced in C++11 and provides a more readable equivalent to the traditional for loop. Let’s look at an example of summing the elements of a vector of integers.

#include <iostream>
#include <vector>

int main() {

    // Vector of int

    std::vector<int> vec{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

    int sum = 0;

    // Get sum of elements using range-based loop

    for (auto var : vec)

        sum += var;

    // Print elements and sum 

    std::cout<< "Vector elements: ";
   
    for (auto i : vec)

        std::cout<<i<<" ";

    std::cout << std::endl;

    std::cout << "Sum of elements in vector:  " << sum << std::endl;

    return 0;

}

Let’s run the code to get the result:

Vector elements: 0 1 1 2 3 5 8 13 21 34 55 
Sum of elements in vector:  143

Sum Elements of a Vector Using for_each

We can use the std::for_each function, which performs a provided function on each element within a specified range. Let’s look at an example of using the for_each function with a sum function.

#include <iostream>
#include <vector>

// Initial sum value

int sum=0;

// Function to sum elements

void func (int val) {
    sum += val;
}

int main() {

    // Vector of int

    std::vector<int> vec{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

    // Call for_each function using sum function 

    std::for_each(vec.begin(), vec.end(), func);

    // Print elements and sum to the console

    std::cout<< "Vector elements: ";
    
    for (auto i : vec)

        std::cout<<i<<" ";

    std::cout << std::endl;

    std::cout << "Sum of elements in vector:  " << sum << std::endl;

    return 0;

}
Vector elements: 0 1 1 2 3 5 8 13 21 34 55 
Sum of elements in vector:  143

Sum Elements of a Vector Using for_each with Lambda

We can use the std::for_each function, which performs a provided function on each element within a specified range. Let’s look at an example of using a lambda function.

#include <iostream>
#include <vector>

int main() {
    int sum = 0;
    
   // Vector of int

    std::vector<int> vec{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55};
]
    // Call for_each function using lambda function 

    std::for_each(vec.begin(), vec.end(), [&] (int n){sum += n;});
    
    // Print elments and sum of elements 

    std::cout<< "Vector elements: ";
   
    for (auto i : vec)

        std::cout<<i<<" ";

    std::cout << std::endl;

    std::cout << "Sum of elements in vector:  " << sum << std::endl;

    return 0;

}
Vector elements: 0 1 1 2 3 5 8 13 21 34 55 
Sum of elements in vector:  143

Sum Elements of a Vector Using an Iterator

We can use the auto keyword to ask the compiler to deduce the variable type from the initialization. Let’s look at an example of summing the elements of a vector using an iterator.

#include <iostream>
#include <vector>

int main() {
    
    // Initial sum value

    int sum =0;

    // Vector of int

    std::vector<int> vec{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

    // Define iterator and sum elments

    for (auto it = vec.begin(); it != vec.end(); ++it){

        sum+= *it;

    }
    // Print elements and sum

    std::cout<< "Vector elements: ";

    for (auto i : vec)

        std::cout<<i<<" ";

    std::cout << std::endl;

    std::cout << "Sum of elements in vector:  " << sum << std::endl;

    return 0;

}

Let’s run the code to

Vector elements: 0 1 1 2 3 5 8 13 21 34 55 
Sum of elements in vector:  143

Summary

Congratulations on reading to the end of this tutorial! We have gone through several ways to check if an element exists in a vector and how to count the number of occurrences of an element in a vector.

For further reading on C++, go to the articles:

If you want to test your C++ code, try out The Research Scientist Pod Code Compiler!

Have fun and happy researching!