std::multiset<Key,Compare,Allocator>::extract
來自 cppreference.com
node_type extract( const_iterator position ); |
(1) | (C++17 起) |
node_type extract( const Key& k ); |
(2) | (C++17 起) |
template< class K > node_type extract( K&& x ); |
(3) | (C++23 起) |
3) 與 (2) 相同。此過載僅在限定 ID Compare::is_transparent 有效且表示一個型別,並且
iterator
和 const_iterator
都不能從 K
隱式轉換時才參與過載決議。它允許在不構造 Key
例項的情況下呼叫此函式。在這兩種情況下,都不會複製或移動元素,只重新指向容器節點的內部指標(可能會發生重新平衡,如同 erase())。
提取節點僅使指向被提取元素的迭代器失效。指向被提取元素的指標和引用仍然有效,但在元素被節點控制代碼擁有時不能使用:如果元素被插入到容器中,它們將變得可用。
目錄 |
[編輯] 引數
position | - | 指向此容器的有效迭代器 |
k | - | 用於標識要提取節點的鍵 |
x | - | 可與要提取節點的鍵進行透明比較的任意型別的值 |
[編輯] 返回值
一個擁有被提取元素的節點控制代碼,如果在 (2,3) 中未找到元素,則為空節點控制代碼。
[編輯] 異常
1) 不丟擲任何異常。
2,3) 由
Compare
物件丟擲的任何異常。[編輯] 複雜度
1) 均攤常數。
2,3) log(size())
[編輯] 注意
extract 是將僅可移動物件從集合中取出的唯一方法
std::set<move_only_type> s; s.emplace(...); move_only_type mot = std::move(s.extract(s.begin()).value());
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_associative_heterogeneous_erasure |
202110L |
(C++23) | 關聯容器和無序關聯容器中的異構擦除,(3) |
[編輯] 示例
執行此程式碼
#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); }
輸出
Start: 1 2 3 After extract and before insert: 2 3 End: 2 3 4
[編輯] 參閱
(C++17) |
從另一個容器拼接節點 (public member function) |
插入元素 或節點(C++17 起) (public member function) | |
擦除元素 (public member function) |