C++官网参考链接:https://cplusplus.com/reference/deque/deque/operator=/
公有成员函数
<deque>
std::deque::operator=
C++98
复制赋值 (1)
deque& operator= (const deque& x);
C++11
复制赋值 (1)
deque& operator= (const deque& x);
移动赋值 (2)
deque& operator= (deque&& x);
初始化列表赋值 (3)
deque& operator= (initializer_list<value_type> il);
分配内容
向容器分配新内容,替换其当前内容,并相应地修改其size。
C++98
将x中的所有元素复制到容器中。
容器保留其当前的allocator(current allocator)。
C++11
复制赋值(1)将x中的所有元素复制到容器中(x保留其内容)。
移动赋值(2)将x的元素移动到容器中(x保持未指定但有效的状态)。
初始化器列表赋值(3)将il的元素复制到容器中。
容器保留其当前的allocator(current allocator),除非allocator_traits指示x的allocator应该传播(propagate)。如果存储需求发生变化,则使用该allocator(通过其traits)进行allocate或deallocate,如果需要,则construct或destroy元素。
调用之前容器中保存的任何元素都被赋值或销毁。
形参
x
一个相同类型的deque对象(即具有相同的模板形参,T和Alloc)。
il
一个initializer_list对象。编译器将从初始化列表声明器自动构造此类对象。
成员类型value_type是容器中元素的类型,在deque中定义为其第一个模板形参(T)的别名。
返回值
*this。
用例
// assignment operator with deques
#include <iostream>
#include <deque>
int main ()
{
std::deque<int> first (3); // deque with 3 zero-initialized ints
std::deque<int> second (5); // deque with 5 zero-initialized ints
second = first;
first = std::deque<int>();
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
return 0;
}
输出:
复杂度
size中的线性。
iterator的有效性
调用之前与此容器相关的所有iterator、reference和指针都将失效。
在移动赋值操作中,指向x中的元素的iterator、指针和reference也会失效。
数据竞争
所有复制的元素都被访问。
移动赋值(2)修改x。
容器及其所有元素都被修改。
异常安全
基本保证:如果抛出异常,则容器处于有效状态。
如果元素结构的适当实参不支持allocator_traits::construct,或者value_type不能复制可赋值(copy assignable)(或对于(2)不能移动可赋值(move assignable)),则会导致未定义行为。