C++ std::set merge() Function

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

Definition

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

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

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

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

C++
std::set<int> a = {1, 2, 3};
std::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::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 set with the union, use std::set_union instead.

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

Parameters

ParameterDescription
sourceThe set or multiset to take values from. Only values that are new to this 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 a set that shares one value with the first set. See which values stay behind.

C++
#include <iostream>
#include <set>

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

int main() {
    std::set<int> a = {1, 2, 3};
    std::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 a 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 <set>
#include <string>

int main() {
    std::set<std::string> a = {"apple"};
    std::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 sorted 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 multiset can hold duplicates. The set takes one copy of each new value, and the rest stay behind.

C++
#include <iostream>
#include <set>

int main() {
    std::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 multiset holds 2, 2, 3. It allows a repeat, which is the main difference from a std::set.
  2. The value 3 is new to the set, so it moves over. Both copies of 2 clash with the 2 already in the set, so they stay in the multiset.
  3. The set ends as 1 2 3 and the multiset keeps 2 2. The set never accepts a second equal value.

Frequently Asked Questions

It moves values from another set into this set, if this 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: rbegin()

Keep going through the std::set functions. Get a reverse iterator to the biggest value.

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.

11 people found this page helpful