std::set<Key,Compare,Allocator>::extract
| node_type extract( const_iterator position ); |
(1) | (since C++17) |
| node_type extract( const key_type& x ); |
(2) | (since C++17) |
position and returns a node handle that owns itx, unlinks the node that contains that element from the container and returns a node handle that owns it. Otherwise, returns an empty node handle.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 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.
Contents |
[edit] Parameters
| position | - | a valid iterator into this container |
| x | - | a key to identify the node to be extracted |
[edit] Return value
A node handle that owns the extracted element, or empty node handle in case the element is not found in overload (2)
[edit] Complexity
[edit] Notes
extract is the only way to take a move-only object out of a set
set<move_only_type> s; s.emplace(...); move_only_type mot = move(s.extract(s.begin()).value());
[edit] Example
#include <algorithm> #include <iostream> #include <set> int main() { std::set<int> cont{1, 2, 3}; auto print = [](const int& n) { std::cout << " " << n; }; std::cout << "Start:"; std::for_each(cont.begin(), cont.end(), print); std::cout << '\n'; // Extract node handle and change key auto nh = cont.extract(1); nh.value() = 4; std::cout << "After extract and before insert:"; std::for_each(cont.begin(), cont.end(), print); std::cout << '\n'; // Insert node handle back cont.insert(move(nh)); std::cout << "End:"; std::for_each(cont.begin(), cont.end(), print); std::cout << '\n'; }
Output:
Start: 1 2 3 After extract and before insert: 2 3 End: 2 3 4
[edit] See also
| (C++17) |
splices nodes from another container (public member function) |
| inserts elements or nodes (since C++17) (public member function) | |
| erases elements (public member function) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
