I would like to copy the content of one std::map into another. Can I use std::copy
for that? Obviously, the following code won't work:
int main() {
typedef std::map<int,double> Map;
Map m1;
m1[3] = 0.3;
m1[5] = 0.5;
Map m2;
m2[1] = 0.1;
std::copy(m1.begin(), m1.end(), m2.begin());
return 0;
}
This won't work because copy
will call operator*
on m2.begin()
to "dereference" it and assign a value (all values are of type std::pair<const int, double>
). Then it will call operator++
to move to the next space in m2
. Both of these operations don't work because of the const
in const int
and there is no space reserved for any new elements.
Is there any way to make it work with std::copy
?
Thanks!