std::set<Key,Compare,Allocator>::merge
来自 cppreference.cn
template< class C2 > void merge( std::set<Key, C2, Allocator>& source ); |
(1) | (since C++17) |
template< class C2 > void merge( std::set<Key, C2, Allocator>&& source ); |
(2) | (since C++17) |
template< class C2 > void merge( std::multiset<Key, C2, Allocator>& source ); |
(3) | (since C++17) |
template< class C2 > void merge( std::multiset<Key, C2, Allocator>&& source ); |
(4) | (since 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) |