跳到主要内容

std::unordered_multiset operator=

// (1) Non const version only
unordered_multiset& operator=( const unordered_multiset& other );

// (2) Non const version only
unordered_multiset& operator=( unordered_multiset&& other ) noexcept(/* see below */);

// (3) Non const version only
unordered_multiset& operator=( std::initializer_list<value_type> ilist );

用另一个容器的内容替换当前容器的内容。

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

参数

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

返回值

*this

复杂度

  • (1) 线性复杂度,与 *thisother 的大小有关 - O(size() + other.size())
  • (2)
    *this 的大小上呈线性 - O(size())
    如果分配器不相等且不传播,则与 *thisother 的大小成线性关系 - O(size() + other.size())
  • (3)*thisilist 的大小成线性关系 - O(size() + ilist.size())

异常

  • (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 的引用、指针和迭代器(除了末尾迭代器)仍然有效,但指向现在属于 *this 的元素。当前标准通过 [container.requirements.general]/12 中的概括性陈述来保证这一点,并且正在通过 LWG 2321 考虑更直接的保证。

示例

Main.cpp
#include <unordered_set>
#include <iterator>
#include <iostream>

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 }
本文来源于 此 CppReference 页面。可能已根据改进或编辑偏好进行了修改。点击“编辑此页面”查看对此文档所做的所有更改。
悬停查看原始许可证。

std::unordered_multiset operator=

// (1) Non const version only
unordered_multiset& operator=( const unordered_multiset& other );

// (2) Non const version only
unordered_multiset& operator=( unordered_multiset&& other ) noexcept(/* see below */);

// (3) Non const version only
unordered_multiset& operator=( std::initializer_list<value_type> ilist );

用另一个容器的内容替换当前容器的内容。

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

参数

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

返回值

*this

复杂度

  • (1) 线性复杂度,与 *thisother 的大小有关 - O(size() + other.size())
  • (2)
    *this 的大小上呈线性 - O(size())
    如果分配器不相等且不传播,则与 *thisother 的大小成线性关系 - O(size() + other.size())
  • (3)*thisilist 的大小成线性关系 - O(size() + ilist.size())

异常

  • (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 的引用、指针和迭代器(除了末尾迭代器)仍然有效,但指向现在属于 *this 的元素。当前标准通过 [container.requirements.general]/12 中的概括性陈述来保证这一点,并且正在通过 LWG 2321 考虑更直接的保证。

示例

Main.cpp
#include <unordered_set>
#include <iterator>
#include <iostream>

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 }
本文来源于 此 CppReference 页面。可能已根据改进或编辑偏好进行了修改。点击“编辑此页面”查看对此文档所做的所有更改。
悬停查看原始许可证。