std::multimap::extract(3)
π std::multimap::extract(3) π
Okay bestie, welcome to the C++ Standard Libary manual page for std::multimap::extract β yes, that's the manual name, and we are respecting it exactly. π
This page is all about the node-handle extract situation, and it's lowkey iconic. π
πΈ NAME πΈ
std::multimap::extractβstd::multimap::extract. It's giving self-titled debut album, and honestly? Ate. π
β¨ 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 );
- Unlinks the node that contains the element pointed to by
positionand returns a node handle that owns it. - If the container has an element with key equivalent to
k, unlinks the node that contains the first such element from the container and returns a node handle that owns it. Otherwise, returns an empty node handle. - Same as (2). This overload participates in overload resolution only if the qualified-id
Compare::is_transparentis valid and denotes a type, and neitheriteratornorconst_iteratoris implicitly convertible fromK. It allows calling this function without constructing an instance ofKey.
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()).
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 element is owned by a node handle: they become usable if the element is inserted into a container.
π«Ά Bestie, clock this: only iterators to the extracted element get invalidated. Pointers and references stay valid, but they're unusable while the node handle owns the element β they come back online if the element gets 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) Throws nothing.
- 2,3) Any exceptions thrown by the
Compareobject.
πΈ Complexity πΈ
- 1) Amortized constant.
- 2,3)
log(size()).
π Notes π
extract is 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"}}
π§ 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 π
The page literally says // Run this code, so go off, bestie:
#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::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);
}
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.
Later, bestie! π«Ά This page came from http://cppreference.com, version/date 2024.06.10, and it's serving std::multimap::extract(3) realness. πβ¨
π¦ 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