Copy std::map data to another map
Copying one map to another can be done with operator = or the copy constructor.
E.g
map<X, Y> mp1;
//fill mp1 with data
map<X, Y> mp2(mp1); //mp2 is a copy of mp1 (via copy-construction)
map<X, Y> mp3;
mp3 = mp2; // mp3 is also a copy of mp2 (via copy-assignment)
The code you've posted above will work correctly assuming that Amap2
is empty. If you try to insert
a key/value pair into a map
that already holds that key, then the old value will be kept and the new one will be discarded. For that reason, if you write
Amap2.insert(Amap1.begin(), Amap1.end());
In some circumstances you might not copy everything over as intended, because duplicate keys won't copy.
To set Amap2
equal to Amap1
, consider just using the assignment operator:
Amap2 = Amap1;
This will blindly discard the contents of Amap2
, though, so be careful when doing this.
If what you want to do is add all the key/value pairs from Amap2
into Amap1
in a way that completely overrides the existing key/value pairs, you can do so using the following logic. The idea here is similar to the logic behind mergesort - we treat the maps as sequences of sorted values and then continuously blend the two together:
void MergeMaps(map<int, A>& lhs, const map<int, A>& rhs) {
map<int, A>::iterator lhsItr = lhs.begin();
map<int, A>::const_iterator rhsItr = rhs.begin();
while (lhsItr != lhs.end() && rhsItr != rhs.end()) {
/* If the rhs value is less than the lhs value, then insert it into the
lhs map and skip past it. */
if (rhsItr->first < lhsItr->first) {
lhs.insert(lhsItr, *rhsItr); // Use lhsItr as a hint.
++rhsItr;
}
/* Otherwise, if the values are equal, overwrite the lhs value and move both
iterators forward. */
else if (rhsItr->first == lhsItr->first) {
lhsItr->second = rhsItr->second;
++lhsItr; ++rhsItr;
}
/* Otherwise the rhs value is bigger, so skip past the lhs value. */
else
++lhsItr;
}
/* At this point we've exhausted one of the two ranges. Add what's left of the
rhs values to the lhs map, since we know there are no duplicates there. */
lhs.insert(rhsItr, rhs.end());
}
With this, you can write
MergeMaps(Amap1, Amap2);
To copy all the key/value pairs from Amap2
into Amap1
.
Hope this helps!