std::map::extract(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

okay bestie, this is the std::map::extract(3) page from the C++ Standard Libary manual β€” yes, that spelling is exactly what it says, and we are keeping every detail, no cap πŸ’–βœ¨

🌸 NAME 🌸

std::map::extract β€” std::map::extract. That's the whole name section, short and iconic πŸŽ€

✨ Synopsis ✨

node_type extract( const_iterator position ); (1) (since C++17)
node_type extract( const Key& k );            (2) (since C++17)
template< class K >                           (3) (since C++23)
node_type extract( K&& x );
  1. (1) unlinks the node that contains the element pointed to by position and returns a node handle that owns it.
  2. (2) if the container has an element with key equivalent to k, unlinks the node that contains that element from the container and returns a node handle that owns it. Otherwise, returns an empty node handle.
  3. (3) same as (2). This overload participates in overload resolution only if the qualified-id Compare::is_transparent is valid and denotes a type, and neither iterator nor const_iterator is implicitly convertible from K. It allows calling this function without constructing an instance of Key.

In either case, no elements are copied or moved β€” only the internal pointers of the container nodes are repointed (rebalancing may occur, as with erase()). Literally just pointer glow-up energy πŸ¦‹

Extracting a node invalidates only the iterators to the extracted element. Pointers and references to the extracted element remain valid, but cannot be used while the element is owned by a node handle: they become usable if the element is inserted into a container.

πŸŽ€ Parameters πŸŽ€

  • position β€” a valid iterator into this container.
  • k β€” a key to identify the node to be extracted.
  • x β€” a value of any type that can be transparently compared with a key identifying the node to be extracted.

πŸ’œ Return value πŸ’œ

A node handle that owns the extracted element, or empty node handle in case the element is not found in (2,3).

πŸ”₯ Exceptions πŸ”₯

  1. (1) throws nothing. Nothing at all, bestie 🫢
  2. (2,3) any exceptions thrown by the Compare object.

⭐ Complexity ⭐

  1. (1) amortized constant.
  2. (2,3) log(size()).

πŸ¦‹ Notes πŸ¦‹

extract is the only way to change a key of a map element without reallocation. This is the main character fact, iykyk πŸ‘‘

std::map<int, std::string> m{{1, "mango"}, {2, "papaya"}, {3, "guava"}};
auto nh = m.extract(2);
nh.key() = 4;
m.insert(std::move(nh));
// m == {{1, "mango"}, {3, "guava"}, {4, "papaya"}}

🌈 Feature-test macro 🌈

  • Feature-test macro: __cpp_lib_associative_heterogeneous_erasure
  • Value: 202110L
  • Std: (C++23)
  • Feature: Heterogeneous erasure in associative containers and unordered associative containers, (3)

πŸ“ Example πŸ“

// Run this code

#include <algorithm>
#include <iostream>
#include <string_view>
#include <map>

void print(std::string_view comment, const auto& data)
{
    std::cout << comment;
    for (auto [k, v] : data)
        std::cout << ' ' << k << '(' << v << ')';

    std::cout << '\n';
}

int main()
{
    std::map<int, char> cont{{1, 'a'}, {2, 'b'}, {3, 'c'}};

    print("Start:", cont);

    // Extract node handle and change key
    auto nh = cont.extract(1);
    nh.key() = 4;

    print("After extract and before insert:", cont);

    // Insert node handle back
    cont.insert(std::move(nh));

    print("End:", cont);
}

🌸 Output 🌸

Start: 1(a) 2(b) 3(c)
After extract and before insert: 2(b) 3(c)
End: 2(b) 3(c) 4(a)

πŸ‘‘ See also πŸ‘‘

  • merge β€” splices nodes from another container (C++17) (public member function).
  • insert β€” inserts elements or nodes (since C++17) (public member function).
  • erase β€” erases elements (public member function).

okay bestie, that's the whole page β€” source/version: http://cppreference.com, date: 2024.06.10, page: std::map::extract(3). Stay slay and keep your node handles safe πŸ’–βœ¨

πŸ“¦ where this page comes from