Use of c ++ --- deque

  1. Introduction deque
  2. deque use
  3. deque applications

First, the introduction and use of deque

  • Introduction deque
  1. the deque (pronounced like "deck"), is a deque irregular Acronym deque sequence is dynamically-sized containers, which can be telescopically image ends.
  2. Deque specific library can be achieved in different ways, but usually a dynamic array. Whatever the case, which allows direct access to individual elements by random access iterators, dynamically scalable according to need.
  3. Thus, the vector provides some deque similar functionality, but deque data insert and delete operations more efficient in the head and tail. The difference is that the vector, deque not guarantee that all the elements stored in a continuous space, access elements in the deque by the pointer is an offset manner may result in an illegal operation.
  4. vector and list provides a similar interface, so it has a similar purpose, but the different implementations of the principles of the inside of: vector that use a dynamic array, which typically requires dynamic growth; the deque the elements may be dispersed in different memory blocks, stored in a deque are some necessary information, any element commonly used in constant direct access to a range of deque, the internal deque implementation complexity than Vector, but extra information such dque growth in some cases a more efficient , especially in the sequence is relatively large, the reallocation is relatively high cost.
  5. In addition to frequent insertion and deletion operations in the outer head or tail, deque worse than the performance of the list and forward_list.

Here Insert Picture Description
Two, deque use

  • deque construction
deque()//构造空的双端队列
deque(n,val=value type());//用n个值为val的元素构造双端队列
deque(InputIterator first, InputIterator last);//用[first, last)的区间构造双端队列
deque(const deque& x);//双端队列的拷贝构造函数
  • deque iterator,
    Here Insert Picture Description
    iterator the deque deque bottom layer was a continuous space illusion, is actually piecewise continuous , in order to maintain its "whole continuous" illusion, falls deque iterator body. The following figure shows the schematic of deque:
    Here Insert Picture Description
[begin(), end()()) ;//begin:容器起始位置   end最后一个元素下一个位置
[rbegin(), rend()) ;//反向迭代器rbegin在end位置,rend在begin
[cbegin(), cend()) ;//const迭代器,与begin和end位置相同,但不能修改其空间内容
[crbegin(), crend()) ;//const反向迭代器,与crbegin在cend位置,crend在cbegin位置
  • the operation capacity of the deque
size();//返回deque有效元素的个数
empty();//检测deque是否为空,是返回true,否则返回false
resize();//将deque中的元素改变到sz,多处的空间用value填充
  • deque element access operations
operator[];//返回deque中n位置上元素的引用
front();//返回deque中首元素的引用
back();//返回deque中最后一个元素的引用
  • modify operation deque
push_back() ;
pop_back() ;//deque的尾插和尾删 
push_front() ;
 pop_front() ;//deque任意位置插入和删除
insert(pos, value) ;
 erase(pos); 删除deque头部元素
swap() ;交换两个deque中的内容
clear() ;将deque中的元素清

example:

#include <iostream>
#include <deque>
using namespace std;
int main(){
	deque<int>q;//构造一个空的deque
	q.push_back(1);//尾插1 2 3
	q.push_back(2);
	q.push_back(3);
	deque<int>::iterator be = q.begin();
	while (be != q.end()){
		cout << *be << endl;
		be++;
	}
	cout << q.front() << endl;
	cout << q.back() << endl;
	q.push_front(0);
	deque<int>::reverse_iterator be1 = q.rbegin();
	while (be1 != q.rend()){
		cout << *be1 << endl;
		be1++;
	}

	system("pause");
	return EXIT_SUCCESS;
}

Three, deque application scenarios

deque and then in sequence containers relatively tasteless, because if only simple storage element, to use a vector, if an arbitrary position of the insertion or deletion operation more elements, to use the list, it is generally rarely used for deque. Deque is the largest application of the standard library as the underlying structure of the stack and queue.

Guess you like

Origin blog.csdn.net/boke_fengwei/article/details/93723902