I have two std::map
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 wi
Here is an example how to do the task with using std::accumulate
#include
#include
The output is
{ 1, 1 } { 2, 2 } { 3, 3 } { 4, 4 }
{ 2, 5 } { 3, 1 } { 5, 5 }
{ 1, 1 } { 2, 7 } { 3, 4 } { 4, 4 } { 5, 5 }
In fact only for the second map there is a need to use std::accumulate. The first map can be simply copied or assigned to m3.
For example
std::map m3 = m1;
m3 = std::accumulate( m2.begin(), m2.end(), m3,
[]( std::map &m, const std::pair &p )
{
return ( m[p.first] +=p.second, m );
} );