Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overwriting map with map possible with <algorithm>?

I would like to do the following but it seems it is not possible. I am no expert on STL.

typedef std::map<int,int> CMap;

CMap m1;
m1[0] = 10;
m1[1] = 11;
m1[2] = 12;

CMap m2;
m2[20] = 30;
m2[21] = 31;
m2[22] = 32;

std::copy( m1.begin(), m1.end(), m2.begin() );

Is there a way to do this using an algorithm (C++98)? Could it be done with transform() or replace()? If yes, how?

Thanks!

like image 742
Lorenz Avatar asked Sep 20 '26 02:09

Lorenz


1 Answers

You can do this:

m2 = m1;

Or even this if you like:

m2.swap(m1);

And there is this too:

std::copy(m1.begin(), m1.end(), std::inserter(m2, m2.end()));
like image 89
Galik Avatar answered Sep 21 '26 17:09

Galik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!