Home C++ Sets Functions extract() Definition In C++17, std::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 a set, because elements are const inside the set. Edit the value through node.value() and insert the node again. 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 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 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 a set and also try a value that is missing.
#include <iostream>
#include <set>
int main() {
std::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 << "set size: " << s.size() << '\n';
return 0;
} node empty: false, value 2
none empty: true
set size: 2 How It Works extract(2) removes 2 from the 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 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 <set>
#include <utility>
int main() {
std::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 a 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 set. Because it is not in the set, the sort order cannot be broken.insert(std::move(node)) puts it back, and the 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 set to a second set without copying the string.
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> a = {"x", "y", "z"};
std::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 sorted 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 set to the other. Frequently Asked Questions What does std::set::extract() do? It removes a value from the 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::set? Elements are const, so use extract(). Take the node out, change node.value(), and insert the node back into the 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::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.
12 people found this page helpful
Helpful Share Copy link Suggestion