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

发布于:2022-11-15 ⋅ 阅读:(360) ⋅ 点赞:(0)

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中的所有元素复制到容器中。
容器保留其当前的分配器(current allocator)。
C++11
复制赋值(1)将x中的所有元素复制到容器中(x保留其内容)。
移动赋值(2)将x的元素移动到容器中(x保持未指定但有效的状态)。
初始化器列表赋值(3)将il的元素复制到容器中。
容器保留其当前的分配器(current allocator),除非分配器特性(allocator traits)指示x的分配器应该传播(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中的线性。 

迭代器有效性
调用之前与此容器相关的所有迭代器、引用和指针都将失效。
移动赋值操作中,指向x中的元素的迭代器、指针和引用也会失效。

数据竞争
所有复制的元素都被访问。
移动赋值(2)修改x
容器及其所有元素都被修改。

异常安全
基本保证:如果抛出异常,则容器处于有效状态。
如果元素结构的适当实参不支持allocator_traits::construct,或者value_type不能复制可赋值(copy assignable)(或移动可赋值(move assignable)对于(2)),则会导致未定义行为


网站公告

今日签到

点亮在社区的每一天
去签到