std::set<Key,Compare,Allocator>::extract
Min standard notice:
- Unlinks the node that contains the element pointed to by position and returns a node handle that owns it.
# Declarations
node_type extract( const_iterator position );
(since C++17)
node_type extract( const Key& k );
(since C++17)
template< class K >
node_type extract( K&& x );
(since C++23)
# 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 empty node handle in case the element is not found in (2,3).
# Notes
extract is the only way to take a move-only object out of a set:
# Example
#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::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);
}