命名空间
变体
操作

std::multiset<Key,Compare,Allocator>::merge

来自 cppreference.cn
< cpp‎ | 容器‎ | multiset
 
 
 
 
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

如果 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::multiset<K>& v)
{
    os << '[' << v.size() << "] {";
    bool o{};
    for (const auto& e : v)
        os << (o ? ", " : (o = 1, " ")) << e;
    return os << " }\n";
}
 
int main()
{
    std::multiset<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: [4] { A, B, B, C }
q: [4] { C, D, E, E }
p.merge(q);
p: [8] { A, B, B, C, C, D, E, E }
q: [0] { }

[编辑] 参见

(C++17)
从容器中提取节点
(公共成员函数) [编辑]
插入元素 或节点(自 C++17 起)
(公共成员函数) [编辑]