std::unorde...ap::extract(3)
π std::unordered_multimap::extract
okay bestie, so this page is from the C++ Standard Library manpages, and it's all about std::unordered_multimap::extract β literally the girlboss move of yoinking a node out of your container without touching the actual elements. no copying, no moving, just vibes and pointer repointing. slay.
β¨ 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 );
so like, there's three flavors of this icon, and here's the tea on each:
- Unlinks the node that contains the element pointed to by
positionand returns a node handle that owns it. periodt. - 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? bestie gets an empty node handle. sorry babe. π«Ά - Same as (2) β but this overload only participates in overload resolution 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. iykyk πΈ
In either case, no elements are copied or moved β only the internal pointers of the container nodes are repointed. literally just a glow-up for your pointers, no relocation stress π
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 again if the element is inserted into a container. like, she's on a little vacation, okay? she'll be back. β¨
π Parameters π
positionβ a valid iterator into this containerkβ a key to identify the node to be extractedxβ 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). no cap, sometimes she's just not there. π
π₯ Exceptions π₯
- (1) Throws nothing. queen of calm. π
- (2, 3) Any exceptions thrown by the
HashandKeyEqualobject. so like, if those go off, that's on them, not on extract.
β Complexity β
- (1, 2, 3) Average case
O(1), worst caseO(size()).
average case is giving effortless, worst case is giving finals week. π
π Notes π
extract is literally the only way to change a key of a map element without reallocation:
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"}}
see how we swapped papaya's key from 2 to 4 without any reallocation drama? iconic. π
Feature-test macro tea β:
__cpp_lib_associative_heterogeneous_erasureβ value202110L, std(C++23), feature: Heterogeneous erasure in associative containers and unordered associative containers, relates to overload(3).
π¦ Example π¦
// Run this code
#include <algorithm>
#include <iostream>
#include <string_view>
#include <unordered_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::unordered_multimap<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);
}
Possible output π
Start: 1(a) 2(b) 3(c)
After extract and before insert: 2(b) 3(c)
End: 2(b) 3(c) 4(a)
look at her go β extracted, key-swapped, reinserted, all without breaking a sweat. main character energy fr fr. π₯
π 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)
aaaaand that's the whole page bestie! π this gem lives over at http://cppreference.com, dated 2024.06.10, under the very iconic title std::unordered_multimap::extract(3). go forth and extract responsibly. mwah! π
β¨π«Ά
π¦ 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