This code moves one element from one unordered_map
to another.
I would like to ask if my code below can be improved.
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <unordered_map>
int main()
{
struct Data
{
int foo;
bool boo;
};
std::unordered_map<std::string, Data> map_A = {{"A", {1, true}}, {"B", {2, false}}};
std::unordered_map<std::string, Data> map_B = {{"C", {3, false}}, {"D", {4, true}}};;
std::string keyToMove = "C";
map_A.insert({keyToMove, map_B[keyToMove]});
map_B.erase(keyToMove);
std::cout << "map_A" << std::endl;
auto it = map_A.begin();
while(it != map_A.end())
{
std::cout<<it->first<<" :: "<<it->second.foo<<std::endl;
it++;
}
std::cout << "map_B" << std::endl;
auto it2 = map_B.begin();
while(it2 != map_B.end())
{
std::cout<<it2->first<<" :: "<<it2->second.foo<<std::endl;
it2++;
}
return 0;
}
Result:
map_A
C :: 3
B :: 2
A :: 1
map_B
D :: 4
Program ended with exit code: 0
trying
in the first sentence might indicate to some people on this site that the code isn't working. Since the results show that the code is working you might want to remove that word. \$\endgroup\$