C++ std::map merge() Function

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

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.

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 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::map<Key, T, C2, Allocator>& source);
template<class C2>
void merge(std::map<Key, T, C2, Allocator>&& source);   // C++17

Parameters

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

C++ Examples

Example 1 — merge() Moves All New Pairs

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

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

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 land in sorted positions, so a prints 1a 2b 3c 4d.
  3. 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.

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

How It Works

  1. Both 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

The source map may sort its keys in a different order. The pairs are still merged into the sorted target.

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

  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 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: rbegin()

Keep going through the std::map functions. Get a reverse iterator to the pair with the biggest key.

rbegin() 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.

13 people found this page helpful