std::unorde...et::extract(3)

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

🌸 NAME 🌸

okay bestie, welcome to the std::unorde...et::extract(3) page from the C++ Standard Library πŸ’– The main character is std::unordered_multiset::extract β€” literally std::unordered_multiset::extract. That's the whole name, icon, no notes.

✨ 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. Unlinks the node that contains the element pointed to by position and returns a node handle that owns it. Slay, the node has left the chat but is still yours.
  2. If the container has an element with key equivalent to k, it unlinks the node that contains the first such element from the container and returns a node handle that owns it. Otherwise, it returns an empty node handle. 😭
  3. Same as (2). This overload participates in overload resolution only if Hash::is_transparent and KeyEqual::is_transparent are valid and each denotes a type, and neither iterator nor const_iterator is implicitly convertible from K. This assumes that such Hash is callable with both K and Key type, and that the KeyEqual is transparent, which, together, allows calling this function without constructing an instance of Key. iykyk πŸ’…

In either case, no elements are copied or moved β€” only the internal pointers of the container nodes are repointed. Lowkey magical.

Extracting a node invalidates only the iterators to the extracted element, and preserves the relative order of the elements that are not erased.

Pointers and references to the extracted element remain valid, but cannot be used while element is owned by a node handle: they become usable if the element is inserted into a container. So valid, but on pause. 🫢

πŸŽ€ 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) Throws nothing.
  • 2,3) Any exceptions thrown by the Hash and KeyEqual object.

πŸ¦‹ Complexity πŸ¦‹

For 1,2,3: average case O(1), worst case O(size()). The vibes are usually fast, but worst case is giving size-of-container energy. πŸ’€

🌈 Notes 🌈

extract is the only way to take a move-only object out of a set:

std::set<move_only_type> s;
s.emplace(...);
move_only_type mot = std::move(s.extract(s.begin()).value());

Feature-test macro tea:

  • 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 πŸ“

The page literally says // Run this code before the snippet:

#include <algorithm>
#include <iostream>
#include <string_view>
#include <unordered_set>

void print(std::string_view comment, const auto& data)
{
    std::cout << comment;
    for (auto datum : data)
        std::cout << ' ' << datum;

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

int main()
{
    std::unordered_multiset<int> cont{1, 2, 3};

    print("Start:", cont);

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

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

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

    print("End:", cont);
}

🎬 Possible output

Start: 1 2 3
After extract and before insert: 2 3
End: 2 3 4

πŸ‘‘ 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)

πŸ’– that's the whole page, bestie! source/version/date tea: http://cppreference.com, 2024.06.10, and the manual name std::unorde...et::extract(3). mwah πŸ’…

πŸ“¦ where this page comes from