std::multiset::extract(3)
π std::multiset::extract π
okay so this is the std::multiset::extract(3) page from the C++ Standard Libary manual β yes, we are in the C++ Standard Library section, bestie, and this function is lowkey a whole node-handle situation. π«Ά
β¨ NAME β¨
std::multiset::extract β that's the name, and the original NAME line is literally std::multiset::extract - std::multiset::extract. 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 );
- Overload (1) β unlinks the node that contains the element pointed to by
positionand returns a node handle that owns it. - Overload (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. - Overload (3) β 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 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 an empty node handle in case the element is not found in (2,3). β¨
π₯ Exceptions π₯
- Throws nothing.
- (2,3) Any exceptions thrown by the
Compareobject.
π Complexity π
- Amortized constant.
- (2,3)
log(size()).
πΈ 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());
the feature-test macro tea is giving:
- Feature-test macro
__cpp_lib_associative_heterogeneous_erasure- Value:
202110L - Std:
(C++23) - Feature: Heterogeneous erasure in associative containers and unordered associative containers,
(3)π
- Value:
β¨ Example β¨
// Run this code
#include <algorithm>
#include <iostream>
#include <string_view>
#include <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::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);
}
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)
mwah π β source/version: http://cppreference.com, date: 2024.06.10, page: std::multiset::extract(3). periodt. π
π¦ 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