不说废话,直接上代码:
#include <numeric>
#include <iostream>
#include <map>
#include <string>
int main()
{
const std::map<std::string, std::size_t> bla = {{"a", 1}, {"b", 3}};
const std::size_t result = std::accumulate(std::begin(bla), std::end(bla), 0,
[](const std::size_t previous, const std::pair<const std::string, std::size_t>& p)
{ return previous + p.second; });
std::cout << result << "\n";
}
参考资料:
https://stackoverflow.com/questions/14073879/how-to-sum-all-values-in-the-stdmap