C++ Reference: Standard C++ Library reference: Containers: deque: deque: operator=

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)进行allocatedeallocate,如果需要,则constructdestroy元素。
调用之前容器中保存的任何元素都被赋值销毁

形参 
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)),则会导致未定义行为

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值