"STL source code analysis" --- deque1

references:

  "STL source code analysis"

...........................................................................................................................................................

A. Deque Overview

       A vector is continuous unidirectional linear space opening, deque is a dual open spatial continuity, i.e., both sides can insert and delete operations (delete vector may be the head of the insertion operation, but the efficiency is poor, not been accepted).

  The following figure shows a schematic view deque:

 

        deque and the maximum difference vector, one deque allowing insertion and removal operation of the head elements in constant time, two deque wherein no so-called concept of capacity, because it is dynamically segmented continuous space in combination, at any time you can add a new paragraph and link up space.
        Although deque also provide Random Access Iterator, but it is not an ordinary pointer iterators, complexity is much higher than the vector, so unless necessary to use a vector as possible. In order to sort deque most efficient, deuqe will first copy all of the elements in the vector, after sorting, and then copied back deque

 

Two. Deque in controller

        deque is a section of the continuous space configuration, once the new increase in the deque front or trailing spaces, some quantitative continuous space was disposed, in series leading and trailing ends. deque biggest task is to quantify these spaces, maintaining the illusion of a continuous, random access and provides an interface to avoid the "reconfiguration, copy, release" cycle, that is, the cost of complex iterative architecture.
        Since it is piecewise continuous linear space, it must have a central control, but in order to maintain the illusion of a continuous data structure design and the like iterator forward and backward operations are troublesome, the deque is achieved than the code vector component or both list much more.
        deque uses a map (not map the STL containers) as a master, where the map is a continuous space, wherein each element is a pointer to another piece continuous linear space, called a buffer. The principal buffer storage space is the deque.

. 1 Template <class T, = the alloc class of Alloc, size_t BUFSIZ = 0 >
 2  class {the deque
 . 3  public:
 . 4      typedef the value_type T;
 . 5      typedef the value_type * pointer;
 . 6      ...
 . 7  protected:
 . 8      typedef pointer * map_pointer;
 . 9  protected:
 10      map_pointer Map; // point map, map space is continuous, in which each element is a pointer to a buffer 
. 11      size_type map_size; // Map can accommodate many pointers 
12      ...
 13 };

 

 

As can be seen from the above, map actually a T **, that is to say it is a pointer, the pointer points to the other, a space is typed point of T. FIG follows:

 

Three. Deque Example (understood)

 

 1 #include <deque>  
 2 #include <iostream>  
 3 #include <algorithm>  
 4 #include <stdexcept>  
 5 using namespace std;  
 6   
 7 void print(int num)  
 8 {  
 9 
10     cout << num << " ";  
11 }  
12   
13 int main()  
14 {  
15     //1. 初始化  
16     deque<int> v;  
17     deque<int>::iterator iv;  
18   
19     v.assign(10, 2);//将10个值为2的元素赋到deque中  
20     cout << v.size() << endl; //返回deque实际含有的元素数量  
21     cout << endl;  
22   
23     //2. 添加  
24     v.push_front(666);  
25     for (int i = 0; i < 10; i++)  
26         v.push_back(i);  
27     for_each(v.begin(), v.end(), print);//需要#include <algorithm>  
28     cout << endl;  
29     cout << v.size() << endl;  
30     cout << endl;  
31   
32 
33 
34     //3. 插入及遍历、逆遍历  
35     v.insert(v.begin() + 3, 99);  
36     v.insert(v.end() - 3, 99);  
37     for_each(v.begin(), v.end(), print);  
38     cout << endl;  
39     for_each(v.rbegin(), v.rend(), print);//在逆序迭代器上做++运算将指向容器中的前一个元素  
40     cout << endl;  
41   
42     //一般遍历写法  
43     for(iv = v.begin(); iv != v.end(); ++iv)  
44         cout << *iv << " ";  
45     cout << endl;  
46     cout << endl;  
47   
48     //4. 删除  
49     v.erase(v.begin() + 3);  
50 
51    for_each(v.begin(), v.end(), print);  
52     cout << endl;  
53     v.insert(v.begin() + 3, 99);//还原  
54   
55     v.erase(v.begin(), v.begin() + 3); //注意删除了3个元素而不是4个  
56     for_each(v.begin(), v.end(), print);  
57     cout << endl;  
58   
59     v.pop_front();  
60     v.pop_back();  
61     for_each(v.begin(), v.end(), print);  
62     cout << endl;  
63     cout << endl;  
64   
65 
66 
67     //5. 查询  
68     cout << v.front() << endl;  
69     cout << v.back() << endl;  
70   
71     //危险的做法,但一般我们就像访问数组那样操作就行  
72     //for (int i = 15; i < 25; i++)  
73         //cout << "Element " << i << " is " << v[i] << endl;  
74     //安全的做法  
75     int i;  
76     try  
77     {  
78         for (i = 15; i < 25; i++)  
79             cout << "Element " << i << " is " << v.at(i) << endl;  
80     }  
81     catch (out_of_range err)//#include <stdexcept>  
82     {  
83         cout << "out_of_range at " << i << endl;  
84     }  
85     cout << endl;  
86   
87     //6. 清空  
88     v.clear();  
89     cout << v.size() << endl;//0  
90     for_each(v.begin(), v.end(), print); //已经clear,v.begin()==v.end(),不会有任何结果。  
91   
92     return 0;  
93 }  

 

Guess you like

Origin www.cnblogs.com/mysky007/p/11280025.html