Home C++ Maps Functions merge() C++ std::map merge() Function Definition In C++17, std::map::merge() moves pairs from another map into this one . It relinks the existing nodes, so no key or value is copied.
Only pairs whose key is not yet in this map are moved. A pair whose key already exists stays in the source map. Include <map> and use -std=c++17 or newer.
This is a member of std::map<Key, T>, a sorted container of key and value pairs . Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Call merge on the map that receives the pairs. Pass the source map.
a.merge(b); // move new keys of b into a
a.merge(std::move(tmp)); // same, from a temporary map Call What it does a.merge(b)Moves from b every pair whose key is not in a. Returns void. a.merge(std::move(b))The same with an rvalue map. Useful for temporaries.
After a.merge(b), the map b is not necessarily empty . It keeps the pairs whose keys were already in a. The source may be a map with a different comparator, or a multimap.
Library declarations template<class C2>
void merge(std::map<Key, T, C2, Allocator>& source);
template<class C2>
void merge(std::map<Key, T, C2, Allocator>&& source); // C++17 Parameters Parameter Description sourceThe map (or multimap) to take pairs from. It must use the same key, value, and allocator types. Its comparator may differ.
Return Value Return Value Description voidNothing is returned. Compare size() of both maps to see what moved.
C++ Examples 1. Merge two maps without clashes 2. Duplicate keys stay behind 3. Merge from a map with another order Example 1 — merge() Moves All New Pairs Merge two maps with different keys. Everything moves and the source becomes empty.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> a = {{1, "a"}, {3, "c"}};
std::map<int, std::string> b = {{2, "b"}, {4, "d"}};
a.merge(b);
for (const auto& kv : a) {
std::cout << kv.first << kv.second << ' ';
}
std::cout << '\n';
std::cout << "b has " << b.size() << " pairs\n";
return 0;
} 1a 2b 3c 4d
b has 0 pairs How It Works Map a has keys 1 and 3. Map b has keys 2 and 4. No key appears in both. a.merge(b) relinks both nodes of b into a. They land in sorted positions, so a prints 1a 2b 3c 4d.Every pair moved, so b is left with 0 pairs. Example 2 — merge() and Duplicate Keys Merge maps that share a key. The shared pair is not moved and not overwritten.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> a = {{1, "a1"}, {2, "a2"}};
std::map<int, std::string> b = {{2, "b2"}, {3, "b3"}};
a.merge(b);
std::cout << "a:";
for (const auto& kv : a) std::cout << ' ' << kv.first << "=" << kv.second;
std::cout << '\n';
std::cout << "b:";
for (const auto& kv : b) std::cout << ' ' << kv.first << "=" << kv.second;
std::cout << '\n';
return 0;
} a: 1=a1 2=a2 3=b3
b: 2=b2 How It Works Both maps contain key 2, but with different values: "a2" in a and "b2" in b. Key 3 is new to a, so its pair moves over. Key 2 clashes, so a keeps its own a2 and the pair in b is not moved. The result: a has three pairs, and b keeps the one pair that could not move, 2=b2. Example 3 — merge() from a Descending Map The source map may sort its keys in a different order. The pairs are still merged into the sorted target.
#include <functional>
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> a = {{1, "one"}};
std::map<int, std::string, std::greater<int>> g = {{3, "three"}, {2, "two"}};
a.merge(g);
for (const auto& kv : a) {
std::cout << kv.first << " ";
}
std::cout << "| g has " << g.size() << " pairs\n";
return 0;
} How It Works Map g uses std::greater<int>, so it stores its keys from big to small: 3, then 2. a.merge(g) is allowed because only the comparator differs. Each node is relinked into a at the place that a's own order requires.The target is ascending, so the output is 1 2 3. Nothing clashed, so g ends up empty. Frequently Asked Questions What does std::map::merge() do? It moves pairs from another map into this map by relinking nodes, so no key or value is copied.
What happens to keys that exist in both maps? The pair in the source map is not moved. The target keeps its own value and the source keeps the clashing pair.
Is the source map empty afterwards? Only if no key clashed. Otherwise it still holds the pairs whose keys were already in the target.
Which C++ version has merge()? C++17.
Can I merge a map with a different comparator? Yes. The source may use another comparator, such as std::greater, or it may be a std::multimap. The key, value, and allocator types must match.
Next: rbegin() Keep going through the std::map functions. Get a reverse iterator to the pair with the biggest key.
rbegin() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion