[C++] How to use the deque function

How to use the deque function in C++

deque stands for double-ended queue. In STL, its implementation is similar to vector and supports random access . The main difference is that the time to insert and delete elements from the beginning of the deque object is fixed , not linear time like in vector. So most operations occur at the beginning and end of the sequence, you should consider using the deque data structure . In addition, in order to achieve the purpose of fixing the time for insert and delete operations at both ends of the deque, the design of the deque object is more complicated than the vector object. Therefore, although both provide random access to elements and linear time insertion and deletion operations in the middle of the sequence, the vector container performs these operations faster .

1. The header file contains

#include<deque>

2. Constructor

std::deque<int> dq;	//创建一个empty的int型队列

std::deque<int> dq(8);  //创建一个有8个元素的int型队列,默认初始化值(value)为0

std::deque<int> dq(8, 50);  //创建一个有8个元素的int型队列,默认初始化值(value)都设为50

std::deque<int> dq(dq.begin(), dq.end()); //通过迭代器创建队列

std::deque<int> dq1(dq);	//通过拷贝构造创建队列

3. Commonly used function methods

Operators []:可以使用[]操作符访问双向队列中单个的元素。

1. assign()	设置双向队列的值

2. at()	返回指定的元素

3. back()	返回最后一个元素

4. begin()	返回指向第一个元素的迭代器

5. clear()	删除所有元素

6. empty()	返回真如果双向队列为空

7. end()	返回指向尾部的迭代器

8. erase()	删除一个元素

9. front()	返回第一个元素

10. get_allocator()	返回双向队列的配置器

11. insert()	插入一个元素到双向队列中

12. max_size()	返回双向队列能容纳的最大元素个数

13. pop_back()	删除尾部的元素

14. pop_front()	删除头部的元素

15. push_back()	在尾部加入一个元素

16. push_front()	在头部加入一个元素

17. rbegin()	返回指向尾部的逆向迭代器

18. rend()	返回指向头部的逆向迭代器

19. resize()	改变双向队列的大小

20. size()	返回双向队列中元素的个数

21. swap()	和另一个双向队列交换元素

Guess you like

Origin blog.csdn.net/phdongou/article/details/114110163