std::unorde...set::extract(3)
Welcome to the std::unorde...set::extract(3) page, fresh from the C++ Standard Libary. This is the manual for std::unordered_set::extract β and yes, the name and the one-liner are literally the same thing, we love a self-referential icon. π
π NAME π
std::unordered_set::extract β std::unordered_set::extract. That's it, that's the name, no notes. β¨
π Synopsis π
okay so here are the signatures, bestie β keep the numbers, they matter:
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
positionand returns a node handle that owns it. Slay. - 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. No key, no node, no drama. - 3) Same as (2), but with a whole bunch of conditions: this overload participates in overload resolution only if
Hash::is_transparentandKeyEqual::is_transparentare valid and each denotes a type, and neitheriteratornorconst_iteratoris implicitly convertible fromK. This assumes that suchHashis callable with bothKandKeytype, and that theKeyEqualis transparent, which, together, allows calling this function without constructing an instance ofKey. That's the transparent-lookup fantasy, iykyk.
In either case, no elements are copied or moved, only the internal pointers of the container nodes are repointed. 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 the element is owned by a node handle: they become usable if the element is inserted into a container. Important vibe check: valid but unusable until reinserted, periodt.
π Parameters π
positionβ a valid iterator into this container. Must be valid, bestie, don't come in here with a invalid iterator.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 an empty node handle in case the element is not found in (2,3). So either you get the goods or you get an empty little handle, no cap.
π Exceptions π
- 1) Throws nothing. Iconic.
- 2,3) Any exceptions thrown by the
HashandKeyEqualobject.
π₯ Complexity π₯
- 1,2,3) Average case
O(1), worst caseO(size()). Fast in the average girlie, linear in the worst-case scenario.
π§ Notes π§
extract is the only way to take a move-only object out of a set. Not me being dramatic, that's just the facts:
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 π
// Run this code
#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_set<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)
xoxo, your Unix bestie π β source: http://cppreference.com, version/date: 2024.06.10, page: std::unorde...set::extract(3) π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms