std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: operator=
|
unordered_map
&
operator
=
(
const
unordered_map
&
other
)
;
|
(1) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
| (2) | ||
|
unordered_map
&
operator
=
(
unordered_map
&&
other
)
;
|
(自 C++11 起)
(直至 C++17) |
|
|
unordered_map
&
operator
=
(
unordered_map
&&
other
)
noexcept ( /* see below */ ) ; |
(自 C++17 起)
(自 C++26 起为 constexpr) |
|
|
unordered_map
&
operator
=
(
std::
initializer_list
<
value_type
>
ilist
)
;
|
(3) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
替换容器中的内容。
令
traits
为
std::
allocator_traits
<
allocator_type
>
:
目录 |
参数
| other | - | 用作数据源的另一个容器 |
| ilist | - | 用作数据源的初始化列表 |
返回值
* this
复杂度
异常
|
2)
noexcept
规范:
noexcept
(
std::
allocator_traits
<
Allocator
>
::
is_always_equal
::
value
&&
std::
is_nothrow_move_assignable
<
Hash
>
::
value
|
(C++17 起) |
注释
在容器移动赋值操作后(重载 ( 2 ) ),除非因分配器不兼容而强制进行逐元素移动赋值,否则指向 other 的引用、指针和迭代器(除尾后迭代器外)仍然有效,但此时它们指向的元素已位于 * this 中。当前标准通过 [container.reqmts]/67 中的总体声明提供此保证,同时正在通过 LWG 2321 提案考虑提供更直接的保证。
示例
以下代码使用 operator = 将一个 std::unordered_map 赋值给另一个:
#include <initializer_list> #include <iostream> #include <iterator> #include <unordered_map> #include <utility> void print(const auto comment, const auto& container){ auto size = std::size(container); std::cout << comment << "{ "; for (const auto& [key, value] : container) std::cout << '{' << key << ',' << value << (--size ? "}, " : "} "); std::cout << "}\n"; } int main() { std::unordered_map<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_map
(公开成员函数) |