std::map<Key,T,Compare,Allocator>::merge
來自 cppreference.com
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 中的元素鍵等效的元素,則該元素不會從 source 中提取。沒有元素被複制或移動,只有容器節點的內部指標被重新指向。所有指向已傳輸元素的指標和引用仍然有效,但現在它們指向 *this,而不是 source。
如果 get_allocator() != source.get_allocator(),則行為未定義。
目錄 |
[編輯] 引數
source | - | 從中傳輸節點的相容容器 |
[編輯] 返回值
(無)
異常
除非比較丟擲異常,否則不丟擲異常。
[編輯] 複雜度
N * log(size() + N)),其中 N 是 source.size()。
[編輯] 示例
執行此程式碼
#include <iostream> #include <map> #include <string> int main() { std::map<int, std::string> ma{{1, "apple"}, {5, "pear"}, {10, "banana"}}; std::map<int, std::string> mb{{2, "zorro"}, {4, "batman"}, {5, "X"}, {8, "alpaca"}}; std::map<int, std::string> u; u.merge(ma); std::cout << "ma.size(): " << ma.size() << '\n'; u.merge(mb); std::cout << "mb.size(): " << mb.size() << '\n'; std::cout << "mb.at(5): " << mb.at(5) << '\n'; for (auto const& kv : u) std::cout << kv.first << ", " << kv.second << '\n'; }
輸出
ma.size(): 0 mb.size(): 1 mb.at(5): X 1, apple 2, zorro 4, batman 5, pear 8, alpaca 10, banana
[編輯] 參閱
(C++17) |
從容器中提取節點 (public member function) |
插入元素 或節點(C++17 起) (public member function) |