名稱空間
變體
操作

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

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

如果 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)
從容器中提取節點
(public member function) [edit]
插入元素 或節點(C++17 起)
(public member function) [edit]