If I have two map<string, int>
s how can I swap an element from each map
?
For example:
map<string, int> ps{ { "triangle", 0 }, { "cross", 1 }, { "square", 2 }, { "circle", 3 } };
map<string, int> xbox{ { "y", 0 }, { "a", 1 }, { "b", 2 }, { "x", 3 } };
swap(move(ps["cross"]), move(xbox["x"]));
The swap
statement is clearly wrong, but that explains what I want to do. After the swap
statement I'd like ps
to contain:
{ "triangle", 0 }
{ "x", 3 }
{ "square", 2 }
{ "circle", 3 }
And xbox
to contain:
{ "y", 0 }
{ "a", 1 }
{ "b", 2 }
{ "cross", 1 }
I expect there is a good way to do this with C++11's move syntax, but if possible I'd like an answer that also describes how to accomplish this on C++03.
map
is implemented as an ordered tree.
You cannot simply replace a key with a new key as it might have to be placed on a different location in the tree. Consequently, you cannot swap.
Delete an re-insert the k-v pairs manually.
(As a sidenote: you haven't even told us what happens with the values...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With