(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
std::unordered_set
Member types | ||||||
Member functions | ||||||
Non-member functions | ||||||
| ||||||
Deduction guides(C++17) |
node_type extract( const_iterator pos); | (1) | (since C++17) (constexpr since C++26) |
node_type extract(const Key& k); | (2) | (since C++17) (constexpr since C++26) |
template<class K> node_type extract( K&& x); | (3) | (since C++23) (constexpr since C++26) |
Hash
andKeyEqual
are bothtransparent, and neitheriterator
norconst_iterator
is implicitly convertible fromK
. This assumes that suchHash
is callable with bothK
andKey
type, and that theKeyEqual
is transparent, which, together, 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 .
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 element is owned by a node handle: they become usable if the element is inserted into a container.
Contents |
pos | - | 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 |
Anode handle that owns the extracted element, or empty node handle in case the element is not found in(2,3).
Hash
andKeyEqual
object.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());
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_associative_heterogeneous_erasure | 202110L | (C++23) | Heterogeneous erasure inassociative containers andunordered associative containers,(3) |
#include <algorithm>#include <iostream>#include <string_view>#include <unordered_set> void print(std::string_view comment,constauto& 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 keyauto 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 3After extract and before insert: 2 3End: 2 3 4
(C++17) | splices nodes from another container (public member function)[edit] |
inserts elementsor nodes(since C++17) (public member function)[edit] | |
erases elements (public member function)[edit] |