std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::operator=
來自 cppreference.com
unordered_multimap& operator=( const unordered_multimap& other ); |
(1) | (C++11 起) |
(2) | ||
unordered_multimap& operator=( unordered_multimap&& other ); |
(C++11 起) (C++17 前) |
|
unordered_multimap& operator=( unordered_multimap&& other ) noexcept(/* 見下文 */); |
(C++17 起) | |
unordered_multimap& operator=( std::initializer_list<value_type> ilist ); |
(3) | (C++11 起) |
替換容器的內容。
1) 複製賦值運算子。用 `other` 內容的副本替換內容。
如果 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value 為 true,則 *this 的分配器將被 other 的副本替換。如果賦值後 *this 的分配器與舊值比較不相等,則舊分配器用於釋放記憶體,然後新分配器用於在複製元素之前分配記憶體。否則,*this 所擁有的記憶體可以在可能的情況下被重用。在任何情況下,最初屬於 *this 的元素可能會被銷燬或被逐元素複製賦值替換。
2) 移動賦值運算子。使用移動語義(即,將 other 中的資料從 other 移動到此容器中)替換內容。之後,other 處於有效但未指定的狀態。
如果 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value 為 true,則 *this 的分配器將由 other 的分配器副本替換。如果為 false 且 *this 和 other 的分配器不相等,則 *this 不能擁有 other 所擁有的記憶體,必須逐個移動賦值每個元素,並根據需要使用自己的分配器分配額外記憶體。在任何情況下,所有最初屬於 *this 的元素要麼被銷燬,要麼被逐元素移動賦值替換。
3) 用初始化列表 ilist 標識的內容替換內容。
目錄 |
[編輯] 引數
其他 | - | 另一個容器,用作資料來源 |
ilist | - | 初始化列表,用作資料來源 |
[編輯] 返回值
*this
[編輯] 複雜度
1) 與 `*this` 和 `other` 的大小呈線性關係。
2) 除非分配器不相等且不傳播,否則與 *this 的大小成線性關係,在這種情況下,與 *this 和 other 的大小成線性關係。
3) 與 *this 和 ilist 的大小呈線性關係。
[編輯] 異常
1-3) 可能丟擲實現定義的異常。 |
(C++17 前) |
1,3) 可能丟擲實現定義的異常。
2) noexcept 規範:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_move_assignable<Hash>::value |
(C++17 起) |
[編輯] 注意
容器移動賦值 (過載 (2)) 後,除非因不相容的分配器強制逐元素移動賦值,指向 `other` 的引用、指標和迭代器(除了尾迭代器)仍然有效,但它們引用現在在 *this 中的元素。當前的 C++ 標準透過 [container.reqmts]/67 中的通用宣告提供此保證,並且 LWG issue 2321 正在考慮更直接的保證。
[編輯] 示例
以下程式碼使用 operator= 將一個 std::unordered_multimap 賦值給另一個
執行此程式碼
#include <initializer_list> #include <iostream> #include <iterator> #include <unordered_map> #include <utility> void print(auto const comment, auto const& container) { auto size = std::size(container); std::cout << comment << "{ "; for (auto const& [key, value] : container) std::cout << '{' << key << ',' << value << (--size ? "}, " : "} "); std::cout << "}\n"; } int main() { std::unordered_multimap<int, int> x{{1,1}, {2,2}, {3,3}}, y, z; const auto w = {std::pair<const int, int>{4,4}, {5,5}, {6,6}, {7,7}}; std::cout << "Initially:\n"; print("x = ", x); print("y = ", y); print("z = ", z); std::cout << "Copy assignment copies data from x to y:\n"; y = x; print("x = ", x); print("y = ", y); std::cout << "Move assignment moves data from x to z, modifying both x and z:\n"; z = std::move(x); print("x = ", x); print("z = ", z); std::cout << "Assignment of initializer_list w to z:\n"; z = w; print("w = ", w); print("z = ", z); }
可能的輸出
Initially: x = { {3,3}, {2,2}, {1,1} } y = { } z = { } Copy assignment copies data from x to y: x = { {3,3}, {2,2}, {1,1} } y = { {3,3}, {2,2}, {1,1} } Move assignment moves data from x to z, modifying both x and z: x = { } z = { {3,3}, {2,2}, {1,1} } Assignment of initializer_list w to z: w = { {4,4}, {5,5}, {6,6}, {7,7} } z = { {7,7}, {6,6}, {5,5}, {4,4} }
[編輯] 參閱
構造 unordered_multimap (public member function) |