名稱空間
變體
操作

std::multimap<Key,T,Compare,Allocator>::merge

來自 cppreference.com
< cpp‎ | 容器‎ | multimap
 
 
 
 
template< class C2 >
void merge( std::map<Key, T, C2, Allocator>& source );
(1) (C++17 起)
template< class C2 >
void merge( std::map<Key, T, C2, Allocator>&& source );
(2) (C++17 起)
template< class C2 >
void merge( std::multimap<Key, T, C2, Allocator>& source );
(3) (C++17 起)
template< class C2 >
void merge( std::multimap<Key, T, 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 <map>
#include <string>
 
int main()
{
    std::multimap<int, std::string> ma{{1, "apple"}, {5, "pear"}, {10, "banana"}};
    std::multimap<int, std::string> mb{{2, "zorro"}, {4, "batman"}, {5, "X"}, {8, "alpaca"}};
    std::multimap<int, std::string> u;
    u.merge(ma);
    std::cout << "ma.size(): " << ma.size() << '\n';
    u.merge(mb);
    std::cout << "mb.size(): " << mb.size() << '\n';
 
    for (auto const& kv : u)
        std::cout << kv.first << ", " << kv.second << '\n';
}

輸出

ma.size(): 0
mb.size(): 0
1, apple
2, zorro
4, batman
5, pear
5, X
8, alpaca
10, banana

[編輯] 參閱

(C++17)
從容器中提取節點
(public member function) [編輯]
插入元素 或節點(C++17 起)
(public member function) [編輯]