std::unordered_multimap operator=
- 自 C++17 起
- C++17 之前
// (1) Non const version only
unordered_multimap& operator=( const unordered_multimap& other );
// (2) Non const version only
unordered_multimap& operator=( unordered_multimap&& other ) noexcept(/* see below */);
// (3) Non const version only
unordered_multimap& operator=( std::initializer_list<value_type> ilist );
// (1) Non const version only
unordered_multimap& operator=( const unordered_multimap& other );
// (2) Non const version only
unordered_multimap& operator=( unordered_multimap&& other );
// (3) Non const version only
unordered_multimap& operator=( std::initializer_list<value_type> ilist );
用另一个容器的内容替换当前容器的内容。
-
(1) 复制赋值运算符。用
other
的内容副本替换内容。如果std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value
为true
,则*this
的分配器将被other
的分配器的副本替换。如果赋值后
*this
的分配器与其旧值不等,则使用旧分配器释放内存,然后使用新分配器分配内存,再复制元素。否则,
*this
拥有的内存可能在可能的情况下被重用。在任何情况下,最初属于*this
的元素可能被销毁或通过元素级复制赋值替换。 -
(2) 移动赋值运算符。使用移动语义替换其他内容(即,其他数据从其他移动到此容器中)。
other
之后处于有效但未指定的状态。
如果 std::allocator_traits<Alloc>::propagate_on_container_move_assignment::value
为 true
,则 *this
的分配器将替换为 other
的分配器的副本。
如果为false,并且*this
和其他的分配器不相等,则*this
不能接管other拥有的内存,必须单独移动分配每个元素,并在需要时使用自己的分配器分配额外的内存。
在任何情况下,最初属于 *this
的所有元素要么被销毁,要么通过逐元素移动赋值替换。
- (3) 用初始化列表
ilist
标识的内容替换内容。
参数
other
- 用作数据源的另一个容器ilist
- 用作数据源的初始化列表
返回值
*this
复杂度
- (1) 在
*this
和other
的大小上呈线性 - O(size() + other.size())。 - (2)
在*this
的大小上呈线性 - O(size())。
如果分配器不相等且不传播,则与*this
和other
的大小成线性关系 - O(size() + other.size())。 - (3) 与
*this
和ilist
的大小成线性关系 - O(size() + ilist.size())。
异常
- 自 C++17 起
- C++17 之前
- (1, 2) 可能抛出实现定义的异常。
- (3) Noexcept 规范
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_move_assignable<Hash>::value
&& std::is_nothrow_move_assignable<Pred>::value)
备注
在容器移动赋值(重载(2))之后,除非不兼容的分配器强制进行元素级别的移动赋值,否则对other的引用、指针和迭代器(end迭代器除外)保持有效,但指向现在位于*this
的元素。当前标准通过[container.requirements.general]/12中的笼统声明来提供此保证,并且通过LWG 2321正在考虑提供更直接的保证。
示例
#include <initializer_list>
#include <unordered_map>
#include <iterator>
#include <iostream>
#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} }