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.
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.
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 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;
}
Output
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 an unspecified 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 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;
}
Output
set: 1 2 3
multiset: 2 2
How It Works
The multiunordered_set holds 2, 2, 3. It allows a repeat, which is the main difference from a std::unordered_set.
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.
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.