C++ std::set extract() Function

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

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().

C++
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
CallWhat 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
C++
node_type extract(const_iterator position);   // C++17
node_type extract(const key_type& key);     // C++17

Parameters

ParameterDescription
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 ValueDescription
node_typeA node handle that owns the removed value. It is empty if the value was not found.

C++ Examples

Example 1 — extract() Removes and Returns a Node

Take one value out of a set and also try a value that is missing.

C++
#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;
}

How It Works

  1. extract(2) removes 2 from the set and wraps it in a node. node.value() reads it, so it prints 2.
  2. extract(99) finds nothing, so none is an empty node. The program only checks empty() and never calls value() on it.
  3. 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.

C++
#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

  1. Values in a set are const, so *s.find(2) = 20 would not compile. Extracting first gives you a node you may edit.
  2. 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.
  3. 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.

C++
#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

  1. 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.
  2. b.insert(...) takes the node and links it into the second set in sorted order. The text itself is never copied.
  3. 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

It removes a value from the set and returns it as a node handle, without copying or destroying it.
C++17.
Elements are const, so use extract(). Take the node out, change node.value(), and insert the node back into the set.
An empty node handle. Check node.empty() before you use node.value().
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

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.

12 people found this page helpful