std::multiset operator=
- 自 C++17 起
- 自 C++11 起
- C++98 起
// (1) Non const version only
multiset& operator=( const multiset& other );
// (2) Non const version only
multiset& operator=( multiset&& other ) noexcept(/* see below */);
// (3) Non const version only
multiset& operator=( std::initializer_list<value_type> ilist );
// (1) Non const version only
multiset& operator=( const multiset& other );
// (2) Non const version only
multiset& operator=( multiset&& other );
// (3) Non const version only
multiset& operator=( std::initializer_list<value_type> ilist );
// (1) Non const version only
multiset& operator=( const multiset& other );
用另一个容器的内容替换当前容器的内容。
-
(1) 复制赋值运算符。用
other
的内容的副本替换当前内容。- 自 C++11 起
如果
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
的分配器将替换为其他分配器的副本。如果为 false,并且 *this
和其他分配器不相等,则 *this
无法拥有其他拥有的内存,必须单独移动分配每个元素,根据需要使用其自己的分配器分配额外内存。
在任何情况下,最初属于 *this
的所有元素要么被销毁,要么通过逐元素移动赋值替换。
- (3) 用初始化列表
ilist
标识的内容替换内容。
参数
other
- 用作数据源的另一个容器ilist
- 用作数据源的初始化列表
返回值
对容器 *this
的引用。
异常
- 自 C++17 起
- C++17 之前
- (1, 3) 可能抛出实现定义的异常。
- (2) noexcept 规范
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_move_assignable<Compare>::value)
复杂度
- (1) 在
*this
和other
的大小上呈线性 - O(size() + other.size())。 - (2)
在*this
的大小上呈线性 - O(size())。
如果分配器不相等且不传播,则在*this
和other
的大小上呈线性 - O(size() + other.size())。 - (3)
通常为 O(N log N),其中 N 是size() + ilist.size()
。如果ilist
根据value_comp()
排序,则呈线性 - O(size())。
备注
在容器移动赋值(重载 (2))之后,除非不兼容的分配器强制逐元素移动赋值,否则对其他引用的引用、指针和迭代器(除了 end 迭代器)仍然有效,但引用的是现在在 *this
中的元素。当前标准通过 [container.requirements.general]/12
中的全面声明作出此保证,并且正在通过 LWG 2321 考虑更直接的保证。
示例
#include <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::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 = { 1, 2, 3 }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { 1, 2, 3 }
y = { 1, 2, 3 }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { 1, 2, 3 }
Assignment of initializer_list w to z:
w = { 4, 5, 6, 7 }
z = { 4, 5, 6, 7 }