C++ std::unordered_set merge() Function

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

Definition

In C++17, std::unordered_set::merge() moves values from another unordered_set into this unordered_set. Each value is moved as a node, so nothing is copied.

A value is moved only if this unordered_set does not already have an equal one. Values that clash stay in the source. Include <unordered_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Pass the other set. It can be an lvalue or an rvalue.

C++
std::unordered_set<int> a = {1, 2, 3};
std::unordered_set<int> b = {3, 4, 5};
a.merge(b);      // a: 1 2 3 4 5   b: 3
CallWhat it does
s.merge(source)Moves every value of source that is not in s. The clashing values stay in source.
s.merge(std::move(source))Same, but for a source you no longer need. Values that clash still stay in it.

The source may be a std::unordered_set, a std::multiset, or another container of the same value, compare, and allocator types. Use merge() when you want to combine and keep the leftovers. If you need a new unordered_set with the union, use std::set_union instead.

Library declarations
C++
template<class C2> void merge(std::unordered_set<Key, C2, Allocator>& source);        // C++17
template<class C2> void merge(std::unordered_set<Key, C2, Allocator>&& source);
template<class C2> void merge(std::multiset<Key, C2, Allocator>& source);

Parameters

ParameterDescription
sourceThe unordered_set or multiunordered_set to take values from. Only values that are new to this unordered_set are moved.

Return Value

Return ValueDescription
voidNothing is returned. Compare size() of both sets to see what moved.

C++ Examples

Example 1 — merge() Combines Two Sets

Merge an unordered_set that shares one value with the first set. See which values stay behind.

C++
#include <iostream>
#include <unordered_set>

void show(const char* label, const std::unordered_set<int>& s) {
    std::cout << label << ':';
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << '\n';
}

int main() {
    std::unordered_set<int> a = {1, 2, 3};
    std::unordered_set<int> b = {3, 4, 5};

    a.merge(b);

    show("a", a);
    show("b", b);
    return 0;
}

How It Works

  1. a has 1, 2, 3 and b has 3, 4, 5. The value 3 is in both.
  2. a.merge(b) moves 4 and 5 into a, since a did not have them. 3 would be a duplicate, so it is not moved.
  3. The helper show() prints a: 1 2 3 4 5 and b: 3. The leftover in b is the proof that an unordered_set never keeps two equal values.

Example 2 — merge() Can Empty the Source

When no values clash, every value moves and the source becomes empty.

C++
#include <iostream>
#include <unordered_set>
#include <string>

int main() {
    std::unordered_set<std::string> a = {"apple"};
    std::unordered_set<std::string> b = {"cherry", "banana"};

    a.merge(b);

    std::cout << "a:";
    for (const std::string& s : a) {
        std::cout << ' ' << s;
    }
    std::cout << '\n';
    std::cout << "b is empty: " << std::boolalpha << b.empty() << '\n';
    return 0;
}

How It Works

  1. apple is in a only. banana and cherry are in b only, so no value clashes.
  2. merge() moves both values from b. They are linked into a in an unspecified order, so the result is apple, banana, cherry.
  3. Nothing was left behind, so b.empty() is true. Even though b is empty, it is still a valid set that you can use again.

Example 3 — merge() from a std::multiset

A multiunordered_set can hold duplicates. The unordered_set takes one copy of each new value, and the rest stay behind.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> s = {1, 2};
    std::multiset<int> ms = {2, 2, 3};

    s.merge(ms);

    std::cout << "set:";
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << "\nmultiset:";
    for (int x : ms) {
        std::cout << ' ' << x;
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The multiunordered_set holds 2, 2, 3. It allows a repeat, which is the main difference from a std::unordered_set.
  2. The value 3 is new to the unordered_set, so it moves over. Both copies of 2 clash with the 2 already in the unordered_set, so they stay in the multiset.
  3. The unordered_set ends as 1 2 3 and the multiunordered_set keeps 2 2. The unordered_set never accepts a second equal value.

Frequently Asked Questions

It moves values from another unordered_set into this unordered_set, if this unordered_set does not already have an equal value. The moved values leave the source.
C++17.
They stay in the source. merge() never overwrites and never creates a duplicate.
No. It moves the nodes, so no value is copied and no memory is allocated for them.
Yes, if the value, compare, and allocator types match. One copy of each new value moves, and the repeats stay in the multiset.

Next: rehash()

Keep going through the std::unordered_set 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.

15 people found this page helpful