# deque文档

deque,她的意思为双端队列(double-ended queue)。逻辑上看，deque和vector都是连续空间。相比于vector只能从尾部增长元素，而deque可在双侧增加元素。但是从存储结构上，deque并非是连续空间存储的。它是分段连续的。对于vector存储（申请的内存）的增长，可以描述为三个步骤1.申请内存2.搬移元素3释放原先的使用的内存。而由于deque其特殊的数据结构，如下图所示，省去这三个步骤。（但是仍然需要申请buffer去存储新元素，不用去搬移旧有元素了）。

deque的组成(数据成员)：

iterator start;
iterator finish;
map_pointer map;
size_type mapsize;//map大小

## deque的构造与析构

default (1) deque(); explicit deque (const allocator_type& alloc);
fill (2) explicit deque (size_type n, const allocator_type& alloc = allocator_type()); deque (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
range (3) template < class InputIterato r> deque (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
copy (4) deque (const deque& x); deque (const deque& x, const allocator_type& alloc);
move (5) deque (deque&& x); deque (deque&& x, const allocator_type& alloc);
initializerlist(6) deque (initializer_list il, const allocator_type& alloc = allocator_type());

= 赋值

copy (1) deque& operator= (const deque& x); //拷贝赋值函数
move (2) deque& operator= (deque&& x); //移动赋值函数
initializer list (3) deque& operator= (initializer_list il);//利用初始化列表进行赋值

assign

range (1) template void assign (InputIterator first, InputIterator last);
fill (2) void assign (size_type n, const value_type& val) ;
initializer list (3) void assign (initializer_list il);

## deque元素访问

### 迭代器(类指针访问方式)

Iterators
begin cbegin returns an iterator to the beginning (public member function)
end cend returns an iterator to the end (public member function)
rbegin crbegin returns a reverse iterator to the beginning (public member function)
rend crend returns a reverse iterator to the end (public member function)

### 元素访问

Element access 函数原型及其说明
at access specified element with bounds checking (public member function),带有边界检查的访问特定元素访问方式 reference at( size_type pos ); const_reference at( size_type pos ) const;
operator[] access specified element (public member function) reference operator[ ] ( size_type pos ); const_reference operator[ ] ( size_type pos ) const;
front access the first element (public member function) reference front(); const_reference front() const;
back access the last element (public member function) reference back(); const_reference back() const;

## 容量与大小

Capacity 函数原型及说明
empty checks whether the container is empty (public member function),判断容器是否为空 bool empty() const;
size returns the number of elements (public member function) size_type size() const;
max_size returns the maximum possible number of elements (public member function)，返回可能的最大元素个数 size_type max_size() const;
shrink_to_fit(C++11) reduces memory usage by freeing unused memory (public member function),可以释放内存 void shrink_to_fit();

## 容器修改

vector<int> v1{1,2,3};
v1.swap((vector<int>){1});//对于vector可行，对于deque不可行，因为没有传入右值参数的函数原型
​

Modifiers 函数原型及说明
clear clears the contents (public member function),清除内容，所有迭代器失效。deque的clear会清除部分申请的内存空间！ void clear();
insert inserts elements (public member function)依据传入参数分类 single element (1) 单元素 iterator insert (iterator position, const value_type& val); fill (2) 填充 void insert (iterator position, size_type n, const value_type& val); range (3) 通过迭代器的范围插入（要求值类型一致） template < class InputIterator > void insert (iterator position, InputIterator first, InputIterator last);
emplace(C++11) constructs element in-place (public member function)在指定位置构造元素 template <class... Args> iterator emplace (const_iterator position, Args&&... args);
erase erases elements (public member function) iterator erase (const_iterator position ); iterator erase (const_iterator first, const_iterator last );
push_back adds an element to the end (public member function) void push_back (const value_type& val); void push_back (value_type&& val);
emplace_backC++11) constructs an element in-place at the end (public member function) template <class... Args> void emplace_back (Args&&... args);
pop_back removes the last element (public member function) void pop_front();
push_front inserts an element to the beginning (public member function) void push_front (const value_type& val); void push_front (value_type&& val);
emplace_frontC++11) constructs an element in-place at the beginning (public member function) template <class... Args> void emplace_front (Args&&... args);
pop_front removes the first element (public member function) void pop_front();
resize changes the number of elements stored (public member function) void resize (size_type n); void resize (size_type n, const value_type& val);
swap swaps the contents (public member function) void swap (deque& x);

## Reference

[1]《STL源码剖析》候捷

[2]C++ STL 体系结构与内核分析--从平地到万丈高楼-----候捷视频课程,https://www.bilibili.com/video/av45108908

11-06 564

02-22 4588

#### 透析Spring(零): 我们为什么要搞懂Spring

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

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