C++ std::map extract() Function

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

Definition

In C++17, std::map::extract() takes a pair out of the map without destroying it. It returns a node handle, a small object that owns the pair.

With the handle you can read key() and mapped(), change the key (normally impossible because keys are const), and then insert the node into the same or another map. No copy of the key or value is made. Include <map> and use -std=c++17 or newer.

This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Extract by key or by iterator. The result is a node handle.

C++
auto nh = m.extract(2);             // by key
auto nh2 = m.extract(m.begin());    // by iterator
if (!nh.empty()) {
    nh.key();       // the key (can be assigned)
    nh.mapped();    // the value
}
CallWhat it does
m.extract(key)Removes the pair with that key and returns its node handle. If the key is missing, the returned handle is empty.
m.extract(pos)Removes the pair that pos points to and returns its node handle.
nh.empty()Tells you if the node handle holds nothing, for example after a failed extract(key).

Inside a node handle you can write nh.key() = newKey. After that, give the handle back with m.insert(std::move(nh)) to store the pair under its new key.

Library declarations
C++
node_type extract(const_iterator pos);
node_type extract(const Key& key);   // C++17

Parameters

ParameterDescription
keyThe key of the pair to extract.
posAn iterator to the pair to extract. It must be valid and not end().

Return Value

Return ValueDescription
node_typeA node handle that owns the pair. It is empty if the key was not found.

C++ Examples

Example 1 — extract() Removes a Pair and Gives You Its Node

Extract a key, read it through the node handle, and try a key that does not exist.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<int, std::string> m = {{1, "one"}, {2, "two"}, {3, "three"}};

    auto nh = m.extract(2);
    std::cout << "extracted: " << nh.key() << " -> " << nh.mapped() << '\n';
    std::cout << "size now: " << m.size() << '\n';

    auto none = m.extract(99);
    std::cout << "extract(99) empty: " << std::boolalpha << none.empty() << '\n';
    return 0;
}

How It Works

  1. m.extract(2) unlinks the pair for key 2 and returns it in nh. The pair is not copied or destroyed.
  2. nh.key() and nh.mapped() read the key and value. The map itself now has two pairs.
  3. Key 99 is not in the map, so extract(99) returns an empty handle, and empty() is true. It does not throw.

Example 2 — extract() to Rename a Key

Keys are const inside a map. A node handle lets you change one without copying the value.

C++
#include <iostream>
#include <map>
#include <string>
#include <utility>

int main() {
    std::map<std::string, int> m = {{"alice", 1}, {"bob", 2}};

    auto nh = m.extract("bob");
    nh.key() = "robert";              // allowed on a node handle
    m.insert(std::move(nh));

    for (const auto& kv : m) {
        std::cout << kv.first << "=" << kv.second << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Inside a map, it->first is const, so you cannot rename a key in place. extract("bob") removes the pair and hands it to you.
  2. nh.key() = "robert" is allowed because the node is now outside the map. The value 2 is not copied.
  3. m.insert(std::move(nh)) puts the node back under its new key. It lands after alice because the map keeps keys sorted.

Example 3 — extract() to Move Between Maps

Move one pair from a map to another without copying it.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<int, std::string> a = {{1, "one"}, {2, "two"}, {3, "three"}};
    std::map<int, std::string> b = {{10, "ten"}};

    b.insert(a.extract(2));

    std::cout << "a:";
    for (const auto& kv : a) std::cout << ' ' << kv.first;
    std::cout << '\n';

    std::cout << "b:";
    for (const auto& kv : b) std::cout << ' ' << kv.first;
    std::cout << '\n';
    return 0;
}

How It Works

  1. a.extract(2) returns a temporary node handle that owns the pair for key 2.
  2. b.insert(...) accepts that node handle and links the node into b. The key and value are never copied.
  3. Map a now has keys 1 and 3. Map b has 2 and 10, in sorted order.

Frequently Asked Questions

It removes a pair from the map and returns it as a node handle, without copying or destroying the key and value.
To change a key (keys are const inside a map), or to move a pair to another map or back again without copying.
extract(key) returns an empty node handle. Check nh.empty() before you read from it.
C++17.
Call m.insert(std::move(nh)) on the same map or on another map with the same type of node.

Next: find()

Keep going through the std::map functions. Search for a key and get an iterator to its pair, 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