C++ std::unordered_map merge() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_map

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.

C++
a.merge(b);               // move new keys of b into a
a.merge(std::move(tmp));  // same, from a temporary map
CallWhat 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
C++
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

ParameterDescription
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 ValueDescription
voidNothing is returned. Compare size() of both unordered_maps to see what moved.

C++ Examples

Example 1 — merge() Moves All New Pairs

Merge two unordered_maps with different keys. Everything moves and the source becomes empty.

C++
#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;
}

How It Works

  1. Map a has keys 1 and 3. Map b has keys 2 and 4. No key appears in both.
  2. a.merge(b) relinks both nodes of b into a. They move into a. Print specific keys with at() if you need stable output.
  3. 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.

C++
#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;
}

How It Works

  1. Both unordered_maps contain key 2, but with different values: "a2" in a and "b2" in b.
  2. 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.
  3. 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.

C++
#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

  1. Map g uses std::greater<int>, so it stores its keys from big to small: 3, then 2.
  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.
  3. The target is ascending, so the output is 1 2 3. Nothing clashed, so g ends up empty.

Frequently Asked Questions

It moves pairs from another map into this unordered_map by relinking nodes, so no key or value is copied.
The pair in the source map is not moved. The target keeps its own value and the source keeps the clashing pair.
Only if no key clashed. Otherwise it still holds the pairs whose keys were already in the target.
C++17.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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