Get sum of all elements present in a container in C++
This post will discuss how to get the sum of all elements present in a container in C++.
1. Using std::accumulate function
The recommended approach uses the standard algorithm std::accumulate defined in the <numeric> header. It accumulates all the values present in the specified range to the specified sum and returns the result.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <numeric> #include <algorithm> int main() { // generate vector [1, 2, 3, 4, 5] int n = 5; std::vector<int> ints(n); std::generate(ints.rbegin(), ints.rend(), [&]{ return n--; }); int sum = std::accumulate(ints.begin(), ints.end(), 0); std::cout << "The sum of the vector elements is " << sum << std::endl; return 0; } |
Output:
The sum of the vector elements is 15
2. Using std::for_each function
Another efficient solution is to use std::for_each that performs a given operation on each of the elements in the specified range. The following code uses lambda expressions that were introduced with C++11.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <vector> #include <numeric> #include <algorithm> int main() { // generate vector [1, 2, 3, 4, 5] int n = 5; std::vector<int> ints(n); std::generate(ints.rbegin(), ints.rend(), [&]{ return n--; }); int sum = 0; std::for_each(ints.begin(), ints.end(), [&] (auto const &i) { sum += i; }); std::cout << "The sum of the vector elements is " << sum << std::endl; return 0; } |
Output:
The sum of the vector elements is 15
3. Using range-based for-loop
Another solution is to use a range-based for-loop to iterate the container and add each encountered element’s value to a result sum. Range-based for-loop was introduced in C++11.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <vector> #include <algorithm> int main() { // generate vector [1, 2, 3, 4, 5] int n = 5; std::vector<int> ints(n); std::generate(ints.rbegin(), ints.rend(), [&]{ return n--; }); int sum = 0; for (auto const &i: ints) { sum += i; } std::cout << "The sum of the vector elements is " << sum << std::endl; return 0; } |
Output:
The sum of the vector elements is 15
4. Using Iterator
We can also use iterators to process each element. The following program uses const_iterator that was introduced with C++11.
For C++98/03, we can easily convert the code to use begin() and end() functions instead that returns an iterator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #include <algorithm> int main() { // generate vector [1, 2, 3, 4, 5] int n = 5; std::vector<int> ints(n); std::generate(ints.rbegin(), ints.rend(), [&]{ return n--; }); int sum = 0; // auto will be std::vector<int>::const_iterator for (auto it = ints.cbegin(); it != ints.cend(); ++it) { sum += *it; } std::cout << "The sum of the vector elements is " << sum << std::endl; return 0; } |
Output:
The sum of the vector elements is 15
That’s all about getting the sum of all elements present in a container in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)