Home C++ Unordered Maps Functions merge() C++ std::unordered_map merge() Function Definition In C++17, std::unordered_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 unordered_map are moved. A pair whose key already exists stays in the source map. Include <unordered_map> and use -std=c++17 or newer.
This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs . Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. 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 unordered_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 unordered_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::unordered_map<Key, T, C2, Allocator>& source);
template<class C2>
void merge(std::unordered_map<Key, T, C2, Allocator>&& source); // C++17 Parameters Parameter Description sourceThe unordered_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 unordered_maps to see what moved.
C++ Examples 1. Merge two unordered_maps without clashes 2. Duplicate keys stay behind 3. Merge from another unordered_map Example 1 — merge() Moves All New Pairs Merge two unordered_maps with different keys. Everything moves and the source becomes empty.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<int, std::string> a = {{1, "a"}, {3, "c"}};
std::unordered_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 move into a. Print specific keys with at() if you need stable output.Every pair moved, so b is left with 0 pairs. Example 2 — merge() and Duplicate Keys Merge unordered_maps that share a key. The shared pair is not moved and not overwritten.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<int, std::string> a = {{1, "a1"}, {2, "a2"}};
std::unordered_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 unordered_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 Pairs move from the source into the target when the key is new. Neither container is sorted.
#include <functional>
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<int, std::string> a = {{1, "one"}};
std::unordered_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::unordered_map::merge() do? It moves pairs from another map into this unordered_map by relinking nodes, so no key or value is copied.
What happens to keys that exist in both unordered_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 unordered_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 an unordered_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: rehash() Keep going through the std::unordered_map functions. Rebuild the table with at least n buckets.
rehash() 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.
8 people found this page helpful
Helpful Share Copy link Suggestion