Namespaces
Variants

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

From cppreference.net

template < class C2 >
void merge ( std:: set < Key, C2, Allocator > & source ) ;
(1) (自 C++17 起)
(自 C++26 起为 constexpr)
template < class C2 >
void merge ( std:: set < Key, C2, Allocator > && source ) ;
(2) (自 C++17 起)
(自 C++26 起为 constexpr)
template < class C2 >
void merge ( std:: multiset < Key, C2, Allocator > & source ) ;
(3) (自 C++17 起)
(自 C++26 起为 constexpr)
template < class C2 >
void merge ( std:: multiset < Key, C2, Allocator > && source ) ;
(4) (自 C++17 起)
(自 C++26 起为 constexpr)

尝试提取(“拼接”) source 中的每个元素,并使用 * this 的比较对象将其插入到 * this 中。

如果 get_allocator ( ) == source. get_allocator ( ) false ,则行为未定义。

不会复制或移动任何元素,仅重新指向容器节点的内部指针。所有指向被转移元素的指针和引用仍然有效,但现在指向的是 * this ,而非 source

目录

参数

源容器 - 用于转移节点的兼容容器

异常处理

除非比较操作抛出异常,否则不会抛出异常。

复杂度

给定 S size ( ) N source. size ( )

1-4) S·log(S+N)

示例

#include <iostream>
#include <set>
// 打印容器内容
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)
从容器中提取节点
(公开成员函数)
插入元素 或节点 (since C++17)
(公开成员函数)