Consider this situation:
void doSmth1(std::map<int,int> const& m);
void doSmth2(std::map<int,int> const& m) {
std::map<int,int> m2 = m;
m2[42] = 47;
doSmth1(m2);
}
The idea is that doSmth2 will call doSmth1 and forward the map it received from its caller. However, it has to add one additional key-value pair (or override it if it is already there). I would like to avoid copying the whole thing just to pass an additional value to doSmth1.
You can’t do that with the standard map. But if your problem is that specific, you might consider passing the new element separately:
Update: If you really just want one map, and copying the map is out of the question, then here’s how you can implement @arrowdodger’s suggestion to make a temporary modification to the original map: