deque two way queue, one of STL series

Deque two-way queue is a kind of continuous linear space with two-way opening, which can insert and delete elements at both ends of the head and tail efficiently. Deque is very similar to vector in interface. The common member functions of deque are listed below:

 

The implementation of deque is complex, and a map will be maintained internally! It is not a map container in STL), that is, a small piece of continuous space. Each element in the space is a pointer, pointing to another (larger) area, which is called a buffer. The buffer is used to store the data in deque. Therefore, deque is slower than vector in random access and traversal of data. Specific deque implementation can refer to STL source code analysis, of course, the SGI STL used in this book is different from the PJ STL used in VS2008. The structure of deque is shown as follows:

 

Due to the space problem, the implementation details of deque are no longer in-depth. Here is an example of deque:

  1. //Two way queue deque  
  2. //by MoreWindows http://blog.csdn.net/morewindows  
  3. #include <deque>  
  4. #include <cstdio>  
  5. #include <algorithm>  
  6. using namespace std;  
  7. int main()  
  8. {  
  9.     deque<int> ideq(20); //Create a deque ideq with 20 elements of default value 0  
  10.     deque<int>::iterator pos;  
  11.     int i;  
  12.   
  13.     //Assign using assign() means assign in the computer  
  14.     for (i = 0; i < 20; ++i)  
  15.         ideq[i] = i;  
  16.       
  17.     //Output deque  
  18.     printf("output deque Medium data:\n");  
  19.     for (i = 0; i < 20; ++i)  
  20.         printf("%d ", ideq[i]);  
  21.     putchar('\n');  
  22.   
  23.     //Add new data at the beginning and end  
  24.     printf("\n Add new data at the beginning and end...\n");  
  25.     ideq.push_back(100);  
  26.     ideq.push_front(i);  
  27.   
  28.     //Output deque  
  29.     printf("\n output deque Medium data:\n");  
  30.     for (pos = ideq.begin(); pos != ideq.end(); pos++)  
  31.         printf("%d ", *pos);  
  32.     putchar('\n');  
  33.   
  34.     //lookup  
  35.     const int FINDNUMBER = 19;  
  36.     printf("\n lookup%d\n", FINDNUMBER);  
  37.     pos = find(ideq.begin(), ideq.end(), FINDNUMBER);  
  38.     if (pos != ideq.end())  
  39.         printf("find %d success\n", *pos);  
  40.     else  
  41.         printf("find failed\n");  
  42.   
  43.     //Delete data at the beginning and end  
  44.     printf("\n Delete data at the beginning and end...\n");  
  45.     ideq.pop_back();  
  46.     ideq.pop_front();  
  47.   
  48.     //Output deque  
  49.     printf("\n output deque Medium data:\n");  
  50.     for (pos = ideq.begin(); pos != ideq.end(); pos++)  
  51.         printf("%d ", *pos);  
  52.     putchar('\n');  
  53.     return 0;  
  54. }  

The operation results are as follows:

One more thing to note. For deque and vector, use erase(pos) and erase(beg,end) as little as possible. Because this will lead to data moving forward after deleting data in the middle, which makes the efficiency low.

 

 

Please indicate the source of reprint, original address: http://blog.csdn.net/morewindows/article/details/6946811

Posted by Fataqui on Mon, 04 May 2020 09:44:45 -0700