命名空间
变体
操作

std::unordered_multiset<Key,Hash,KeyEqual,Allocator>::operator=

来自 cppreference.cn
 
 
 
 
unordered_multiset& operator=( const unordered_multiset& other );
(1) (since C++11)
(2)
unordered_multiset& operator=( unordered_multiset&& other );
(since C++11)
(until C++17)
unordered_multiset& operator=( unordered_multiset&& other ) noexcept(/* see below */);
(since C++17)
unordered_multiset& operator=( std::initializer_list<value_type> ilist );
(3) (since C++11)

替换容器的内容。

1) 复制赋值运算符。用 other 内容的副本替换内容。
如果 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::valuetrue,则 *this 的分配器会被 other 的副本替换。如果赋值后 *this 的分配器与其旧值不相等,则旧分配器用于释放内存,然后新分配器用于在复制元素之前分配内存。 否则,当可能时,可以重用 *this 拥有的内存。 在任何情况下,最初属于 *this 的元素可能会被销毁或被逐元素复制赋值替换。
2) 移动赋值运算符。使用移动语义用 other 的内容替换内容(即,other 中的数据从 other 移动到此容器中)。 之后 other 处于有效但不指定的状态。
如果 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::valuetrue,则 *this 的分配器会被 other 的分配器的副本替换。 如果为 false*thisother 的分配器不相等,则 *this 无法获得 other 拥有的内存的所有权,并且必须单独移动赋值每个元素,并根据需要使用自己的分配器分配额外的内存。 在任何情况下,最初属于 *this 的所有元素都会被销毁或被逐元素移动赋值替换。
3) 用初始化器列表 ilist 标识的内容替换内容。

内容

[edit] 参数

other - 用作数据源的另一个容器
ilist - 用作数据源的初始化器列表

[edit] 返回值

*this

[edit] 复杂度

1)*thisother 的大小呈线性关系。
2)*this 的大小呈线性关系,除非分配器不相等且不传播,在这种情况下,与 *thisother 的大小呈线性关系。
3)*thisilist 的大小呈线性关系。

[edit] 异常

1-3) 可能会抛出实现定义的异常。
(until C++17)
1,3) 可能会抛出实现定义的异常。
2)
noexcept 规范:  
noexcept(std::allocator_traits<Allocator>::is_always_equal::value

&& std::is_nothrow_move_assignable<Hash>::value

&& std::is_nothrow_move_assignable<Pred>::value)
(since C++17)

[edit] 注意

在容器移动赋值(重载 (2))之后,除非元素级移动赋值是由不兼容的分配器强制执行的,否则对 other 的引用、指针和迭代器(末尾迭代器除外)仍然有效,但引用现在位于 *this 中的元素。 当前标准通过 [container.reqmts]/67 中的一揽子声明做出此保证,并且正在通过 LWG issue 2321 考虑更直接的保证。

[edit] 示例

以下代码使用 operator= 将一个 std::unordered_multiset 分配给另一个

#include <initializer_list>
#include <iostream>
#include <iterator>
#include <unordered_set>
 
void print(auto const comment, auto const& container)
{
    auto size = std::size(container);
    std::cout << comment << "{ ";
    for (auto const& element : container)
        std::cout << element << (--size ? ", " : " ");
    std::cout << "}\n";
}
 
int main()
{
    std::unordered_multiset<int> x{1, 2, 3}, y, z;
    const auto w = {4, 5, 6, 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, 2, 1 }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { 3, 2, 1 }
y = { 3, 2, 1 }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { 3, 2, 1 }
Assignment of initializer_list w to z:
w = { 4, 5, 6, 7 }
z = { 7, 6, 5, 4 }

[edit] 参见

构造 unordered_multiset
(公共成员函数) [编辑]