STL's Sequence Container deque

Source: Internet
Author: User

First look at the template declaration of Deque:

Template <class T, class Alloc = allocator<t>>//Originally also has a parameter bufsize, now the new version built-in, not allow the user to specify.

Classdeque {//... ..protected://data membersIterator start;//performance of the first nodeIterator finish;//Show Last nodeMap_pointer map;//point to map, map is a block of contiguous space, where each element is a reference//pin, point to a bufferSize_type map_size;//How many pointers can be accommodated in map...};

Vector is a continuous space of one-way openings, and deque is a continuous space of bidirectional openings (logically). The so-called two-way openings mean that the insertion (constant order) and deletion (constant order) of the elements can be done at both ends. Of course, vectors are also possible, but their head operations are too inefficient (linear order) and cannot be accepted.

Why is it just logically a continuum of space? The reason is that the deque is actually a paragraph of continuous space in series, and to create the illusion of continuous space, in order to this phenomenon, its iterators really cost a lot of effort.

Deque accepts 2 (previous versions of 3) template parameters, where the latter (previous 2) is the default parameter, typically just write the element type, such as:

deque<int>  deq;

So what does the BufSize parameter (which we still know) be used for? Let's take a look at how the deque is structured.

, Deque uses a so-called map as the main control. A map is a small contiguous space in which each element is a pointer to another large contiguous linear space called a buffer. The buffer is the storage space body of the deque.

deque <int, allocator<int>>  DEQ;

(Image from Houtie Teacher's course "STL and generic Programming", Invasion and deletion)

The deque consists of 2 iterators, as shown in start and finish, each with 4 pointers, cur, first, last, node. Cur, like start, points to the first element of the container, first points to the head of the buffer where the element is located, and last points to the end of the buffer where the element is located. Control the forward and backward of the map via pointer node. When map usage is full, find a larger space to control the buffer as a map,map, that's all. Of course, the construction of the illusion of continuous space, the implementation of the iterator is more troublesome, here is not introduced, we only do understand.

After simply understanding the structure of the deque, let's write a test code:

#include <deque>#include<iostream>#include<algorithm>using namespacestd;intMain () {deque<int, allocator<int>> Ideq ( -,9); cout<<"size ="<< ideq.size () << Endl;//size = 20 (20 elements, all 9)//set a new value for each element     for(inti =0; I < ideq.size (); i++) {Ideq[i]=i; }          for(inti =0; I < ideq.size (); i++) {//0 1 2 3 ... +cout << Ideq[i] <<" "; } cout<<Endl;  for(inti =0; I <3; i++) {ideq.push_back (i); }         for(inti =0; I < ideq.size (); i++) {//0 1 2 3 ... 0 1 2cout << Ideq[i] <<" "; } cout<<"size ="<< ideq.size () << Endl;//size =Ideq.push_front ( About);  for(inti =0; I < ideq.size (); i++) {//99 0 1 2 3 ... 0 1 2cout << Ideq[i] <<" "; } cout<<"size ="<< ideq.size () << Endl;//size =deque<int, allocator<int>>:: Iterator iter; ITER= Find (Ideq.begin (), Ideq.end (), About); cout << *iter << Endl; // About             return 0; } 

Of course, with the compiler version of the update, the internal details will be some changes, such as the new version of the cancellation of the user-defined buffersize, to internal self-implementation. But the fundamentals and how they are stored do not change. Moreover deque container complex, also can not use so short text and the code to explain, here just introduce deque basic storage structure and use, if have the interest deepen deepen to understand the recommendation "STL Source Analysis", also recommend directly open System header file self-read.

STL's Sequence Container deque

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.