std::set<Key,Compare,Allocator>::merge
來自 cppreference.com
template< class C2 > void merge( std::set<Key, C2, Allocator>& source ); |
(1) | (C++17 起) |
template< class C2 > void merge( std::set<Key, C2, Allocator>&& source ); |
(2) | (C++17 起) |
template< class C2 > void merge( std::multiset<Key, C2, Allocator>& source ); |
(3) | (C++17 起) |
template< class C2 > void merge( std::multiset<Key, C2, Allocator>&& source ); |
(4) | (C++17 起) |
嘗試提取(“拼接”) source 中的每個元素,並使用 *this 的比較物件將其插入到 *this 中。如果 *this 中存在一個鍵等同於 source 中某個元素的鍵的元素,則該元素不會從 source 中提取。不進行元素的複製或移動,只重新指向容器節點的內部指標。所有指向已傳輸元素的指標和引用保持有效,但現在它們指向 *this,而不是 source。
如果 get_allocator() != source.get_allocator(),則行為未定義。
目錄 |
[編輯] 引數
source | - | 從中傳輸節點的相容容器 |
[編輯] 返回值
(無)
異常
除非比較丟擲異常,否則不丟擲異常。
[編輯] 複雜度
N * log(size() + N)),其中 N 是 source.size()。
[編輯] 示例
執行此程式碼
#include <iostream> #include <set> // print out a container template<class Os, class K> Os& operator<<(Os& os, const std::set<K>& v) { os << '[' << v.size() << "] {"; bool o{}; for (const auto& e : v) os << (o ? ", " : (o = 1, " ")) << e; return os << " }\n"; } int main() { std::set<char> p{'C', 'B', 'B', 'A'}, q{'E', 'D', 'E', 'C'}; std::cout << "p: " << p << "q: " << q; p.merge(q); std::cout << "p.merge(q);\n" << "p: " << p << "q: " << q; }
輸出
p: [3] { A, B, C } q: [3] { C, D, E } p.merge(q); p: [5] { A, B, C, D, E } q: [1] { C }
[編輯] 參閱
(C++17) |
從容器中提取節點 (public member function) |
插入元素 或節點(C++17 起) (public member function) |