C++ std::unordered_set extract() Function

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

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

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 unordered_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 an unordered_set and also try a value that is missing.

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

How It Works

  1. extract(2) removes 2 from the unordered_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 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.

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

  1. Values in an unordered_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 unordered_set. Because it is not in the unordered_set, the sort order cannot be broken.
  3. 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.

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

  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 an unspecified 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 unordered_set to the other.

Frequently Asked Questions

It removes a value from the unordered_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 unordered_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::unordered_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.

13 people found this page helpful