Copy entries of a map to another map in C++
This post will discuss how to copy entries of a map to another map in C++.
1. Using std::map::insert
The standard approach to copy elements from a map to an “existing” map in C++ is using the std::map::insert member function. For example, the following code will insert all the elements from the beginning to the end of the first map into the second map.
This assumes that the map does not contain any common keys. In case a key already exists in the map, the new value will be discarded, and the old value will be retained.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <string> #include <map> int main() { std::map<std::string, int> first_map = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> second_map = { {"four", 4} }; second_map.insert(first_map.begin(), first_map.end()); for (const auto &entry: second_map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{four, 4}
{one, 1}
{three, 3}
{two, 2}
2. Using Map Constructor
Another good option is to use the copy constructor to construct a map with a copy of each of the elements in the specified map. This is the preferred way to clone a map, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> #include <map> int main() { std::map<std::string, int> first_map = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> second_map(first_map); for (const auto &entry: second_map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{one, 1}
{three, 3}
{two, 2}
This is equivalent to the following code using the assignment operator:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> #include <map> int main() { std::map<std::string, int> first_map = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> second_map = first_map; for (const auto &entry: second_map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{one, 1}
{three, 3}
{two, 2}
Since C++17, we can omit the map’s template arguments and let the compiler deduce them:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> #include <map> int main() { std::map<std::string, int> first_map = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map second_map = first_map; for (const auto &entry: second_map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{one, 1}
{three, 3}
{two, 2}
3. Using std::map::merge
Starting with C++17, we can use the std::map::merge member function to copy content from a map to another map. This is the recommended way to copy the contents of a map to an existing map in C++17 and above.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <string> #include <map> int main() { std::map<std::string, int> first_map = { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> second_map = { {"four", 4} }; second_map.merge(first_map); for (const auto &entry: second_map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{four, 4}
{one, 1}
{three, 3}
{two, 2}
That’s all about copying entries of a map to another map in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)