Home C++ Sets Functions merge() C++ std::set merge() Function 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.
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 Call What 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 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 Parameter Description sourceThe set or multiset to take values from. Only values that are new to this set are moved.
Return Value Return Value Description voidNothing is returned. Compare size() of both sets to see what moved.
Important
merge() needs C++17 . The source is changed : moved values disappear from it, and clashing values stay. Nothing is overwritten and no value is copied. All iterators to moved values stay valid, but they now point into the target set. If the source and the target are the same object, nothing happens.
C++ Examples 1. Merge two sets 2. Merge with no overlap 3. Merge a multiset into a set Example 1 — merge() Combines Two Sets Merge a set that shares one value with the first set. See which values stay behind.
#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 a has 1, 2, 3 and b has 3, 4, 5. The value 3 is in both.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.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.
#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;
} a: apple banana cherry
b is empty: true How It Works apple is in a only. banana and cherry are in b only, so no value clashes.merge() moves both values from b. They are linked into a in sorted order, so the result is apple, banana, cherry.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.
#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 The multiset holds 2, 2, 3. It allows a repeat, which is the main difference from a std::set. 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. The set ends as 1 2 3 and the multiset keeps 2 2. The set never accepts a second equal value. Frequently Asked Questions What does std::set::merge() do? 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.
Which C++ version has merge()? C++17.
What happens to values that already exist in the target set? They stay in the source. merge() never overwrites and never creates a duplicate.
Does merge() copy the values? No. It moves the nodes, so no value is copied and no memory is allocated for them.
Can I merge a multiset into a set? 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 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.
11 people found this page helpful
Helpful Share Copy link Suggestion