"STL Source Analysis" Reading notes of the Sequence container (2)

Source: Internet
Author: User

1.deque

The biggest difference between deque and vectors is that deque allows the insertion and deletion of elements into the first end within a constant time. And Deque does not have the concept of capacity, because it is dynamically in a segmented contiguous space combination, can add a new space at any time and link up. It's not going to happen in deque to reconfigure a larger space as a vector, due to the lack of old space. Although Deque also provides the random Access Iterator, its iterators are not normal pointers, which affects the efficiency of many operations.

(1) Map of Deque

Deque is logically a contiguous space, but in fact it is composed of a section of quantitative continuous space. Once it is necessary to add new space at the front or end of the DEUQE, a fixed amount of contiguous space is configured and threaded to the head or end of the entire deque. The biggest task of deque is to maintain the illusion of its overall continuity in the quantitative continuous space of these segments, and to provide a random access interface. This avoids the vector "re-application of memory, replication , Release " of the cycle, the cost is a complex iterator architecture.

Deque uses a so-called map as the main control. Here the so-called map is a small piece of contiguous space. Each of these elements is a pointer to another section of larger contiguous space, called a buffer. The buffer is the storage body of the deque. The STL allows us to specify the buffer size and, if not specified, uses the 512bytes buffer by default.

Definition of deque:


The map object defined in Deque is actually a t**, that is, it is a pointer, and a pointer to a space of type T, as shown:

(2) Deque iterator

Deque is a segmented contiguous space. The task of maintaining its "overall continuous" illusion falls on the operator++ and operator--two operators of iterators. The data structure of the Deque iterator is designed as follows:

The deque map, buffer, and iterator relate to each other as follows:

Suppose there is now a deque<int>, and make its buffer size 32. After some operations, Deque has 20 elements. Each int occupies 4 bytes of space, and each buffer can have 8 int. 20 int will occupy 3 buffers, and the third buffer will have the remaining space of 16bytes. The memory usage of deque at this time is as follows:



Due to deque special memory structure, when the pointer operation, encountered buffer edge, to pay special attention to, sometimes may need to jump to another buffer (implementation of the reference "STL source Resolution" 148 pages).

(3) data structure of deque

Deque in addition to maintaining a pointer to a previously mentioned map, it also maintains a start,finish two iterator that points to the first element of the first buffer and the last element of the last cache. In addition, it should also remember the size of the current map. Because the map does not provide enough nodes, you must reconfigure a larger map.


2.stack

Stack is an advanced data structure. It has only one exit. Stack allows new elements to be removed, removing elements and getting the top element. But beyond the top, there is no other way to access other elements of the stack.

The deque is a two-way open data structure that can form a stack by deque the bottom structure and enclosing its head-end openings.

Stack is the deque for the bottom of all its work, such as the stack of "modify something interface, form another style" class is called adapter. Its source code is as follows:


"STL Source Analysis" Reading notes of the Sequence container (2)

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.