Merge two maps, summing values for same keys in C++

Viewed 8653

I have two std::map<int,int> maps and wish to merge them into a third map like this: if the same key is found in both maps, create a pair in the third map with the same key and a value which a sum of values from the first and second map, otherwise just copy a pair to the third map. I suspect it can be done with std::accumulate, but I don't understand it well enough.

3 Answers
Related