Find sum of all values in the map in C++
This post will discuss how to find the sum of all values in the map in C++.
1. Using std::accumulate
A simple solution to find the sum of all values in the map is using the std::accumulate function, defined in the <numeric> header. The idea is to overwrite its default behavior with a binary predicate passed in the optional fourth parameter, which extracts the second element of each entry and accumulates its sum. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <map> #include <numeric> int main() { std::map<std::string, int> map = { {"two", 2}, {"one", 1}, {"three", 3}, {"four", 4}, {"five", 5} }; int total_sum = std::accumulate(map.begin(), map.end(), 0, [](const int prev_sum, const std::pair<std::string, int> &entry) { return prev_sum + entry.second; }); std::cout << total_sum << std::endl; // 15 return 0; } |
Since C++14, we can improve the readability of the binary predicate with auto keyword:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <map> #include <numeric> int main() { std::map<std::string, int> map = { {"two", 2}, {"one", 1}, {"three", 3}, {"four", 4}, {"five", 5} }; int total_sum = std::accumulate(map.begin(), map.end(), 0, [](auto prev_sum, auto &entry) { return prev_sum + entry.second; }); std::cout << total_sum << std::endl; // 15 return 0; } |
For multiple invocations to the std::accumulate function, we can construct a class/struct with operator() overload (a functor).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <map> #include <numeric> struct AccumulateMapValues { template<class V, class Pair> V operator()(V value, const Pair &pair) const { return value + pair.second; } }; int main() { std::map<std::string, int> map = { {"two", 2}, {"one", 1}, {"three", 3}, {"four", 4}, {"five", 5} }; int total_sum = std::accumulate(map.begin(), map.end(), 0, AccumulateMapValues()); std::cout << total_sum << std::endl; // 15 return 0; } |
2. Using boost library
Alternatively, we can use Boost.Range library to find the sum of all values in the map. It has the boost::accumulate algorithm, which can be used with adaptors to find the desired sum. The following code example shows the invocation of this function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> #include <boost/range/numeric.hpp> #include <boost/range/adaptor/map.hpp> int main() { std::map<std::string, int> map = { {"two", 2}, {"one", 1}, {"three", 3}, {"four", 4}, {"five", 5} }; int total_sum = boost::accumulate(map | boost::adaptors::map_values, 0); std::cout << total_sum << std::endl; // 15 return 0; } |
That’s all about finding the sum of all values in the map 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 :)