Home C++ Unordered Sets Functions extract() Definition In C++17, std::unordered_set::extract() removes a value and gives it back as a node handle . The value is not copied or destroyed. You own it now.
This is the only legal way to change a value that is stored in an unordered_set, because elements are const inside the unordered_set. Edit the value through node.value() and insert the node again. 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 a value or an iterator. Read the node with value() and check it with empty().
auto node = s.extract(2); // by value
if (!node.empty()) {
node.value() = 20; // edit the value
s.insert(std::move(node)); // put it back
}
auto n2 = s.extract(s.begin()); // by iterator Call What it does s.extract(value)Removes the value and returns a node handle. If the value is missing, the node is empty. s.extract(pos)Removes the element at pos and returns its node handle. pos must be valid.
A node handle is a move-only object. It can be passed to insert() of any set that has the same value type and allocator, even another set, without copying the value. After a successful insert, the handle is empty.
Library declarations node_type extract(const_iterator position); // C++17
node_type extract(const key_type& key); // C++17 Parameters Parameter Description valueThe value to take out of the unordered_set. posA valid iterator to the element to take out. It must not be end().
Return Value Return Value Description node_typeA node handle that owns the removed value. It is empty if the value was not found.
Important
extract() needs C++17 . If the value is not found, you get an empty node , so always check node.empty() before calling node.value(). Reading value() of an empty node is undefined behavior. Compare with erase(), which destroys the value, and with std::map, where the key() of a node can be changed in the same way.
C++ Examples 1. Extract a value 2. Change a stored value 3. Move a value to another set Example 1 — extract() Removes and Returns a Node Take one value out of an unordered_set and also try a value that is missing.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s = {1, 2, 3};
auto node = s.extract(2);
auto none = s.extract(99);
std::cout << std::boolalpha;
std::cout << "node empty: " << node.empty() << ", value " << node.value() << '\n';
std::cout << "none empty: " << none.empty() << '\n';
std::cout << "unordered_set size: " << s.size() << '\n';
return 0;
} node empty: false, value 2
none empty: true
unordered_set size: 2 How It Works extract(2) removes 2 from the unordered_set and wraps it in a node. node.value() reads it, so it prints 2.extract(99) finds nothing, so none is an empty node. The program only checks empty() and never calls value() on it.The unordered_set shrinks to 1 and 3, so its size is 2. The extracted value is still alive inside node. Example 2 — extract() Edits a Value in a Set Take a value out, change it, and put it back. It lands in its new sorted place.
#include <iostream>
#include <unordered_set>
#include <utility>
int main() {
std::unordered_set<int> s = {1, 2, 3};
auto node = s.extract(2);
node.value() = 20;
s.insert(std::move(node));
std::cout << "set:";
for (int x : s) {
std::cout << ' ' << x;
}
std::cout << '\n';
return 0;
} How It Works Values in an unordered_set are const, so *s.find(2) = 20 would not compile. Extracting first gives you a node you may edit. node.value() = 20 changes the value while it is outside the unordered_set. Because it is not in the unordered_set, the sort order cannot be broken.insert(std::move(node)) puts it back, and the unordered_set places 20 at the end. The result is 1 3 20. No copy of the value was made along the way.Example 3 — extract() Between Two Sets Move one name from a first unordered_set to a second set without copying the string.
#include <iostream>
#include <unordered_set>
#include <string>
int main() {
std::unordered_set<std::string> a = {"x", "y", "z"};
std::unordered_set<std::string> b = {"w"};
b.insert(a.extract("y"));
std::cout << "a:";
for (const std::string& s : a) {
std::cout << ' ' << s;
}
std::cout << "\nb:";
for (const std::string& s : b) {
std::cout << ' ' << s;
}
std::cout << '\n';
return 0;
} How It Works a.extract("y") returns a temporary node that owns the text y. The node is an rvalue, so it can go straight into insert() with no std::move.b.insert(...) takes the node and links it into the second set in an unspecified order. The text itself is never copied.Set a now holds x and z. Set b holds w and y. The value moved from one unordered_set to the other. Frequently Asked Questions What does std::unordered_set::extract() do? It removes a value from the unordered_set and returns it as a node handle, without copying or destroying it.
Which C++ version has extract()? C++17.
How do I change a value that is inside a std::unordered_set? Elements are const, so use extract(). Take the node out, change node.value(), and insert the node back into the unordered_set.
What does extract() return if the value is not found? An empty node handle. Check node.empty() before you use node.value().
What is the difference between extract() and erase()? erase() destroys the value. extract() keeps the value alive in a node so you can edit it or move it into another set.
Next: find() Keep going through the std::unordered_set functions. Search for a value and get an iterator to it, or end().
find() 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.
13 people found this page helpful
Helpful Share Copy link Suggestion