Deque of STL containers

Source: Internet
Author: User

"Vector is the continuous linear space of one-way open, while deque is the continuous space of two-way open. The so-called bidirectional opening means that you can insert and delete elements at the beginning and end. Of course, vector can also be operated at both ends of the head and tail (from a technical point of view), but its head operation efficiency is odd and unacceptable ."

"The biggest difference between deque and vector is that deque allows you to insert and remove elements from the start end within the constant time, And deque does not have the so-called capacity concept, because it is dynamically combined with consecutive segments, a new space can be added and linked at any time. In other words, for example, if a vector does not have enough space, it will not happen in deque to re-allocate a larger space and then copy elements, and then release the space ."

"Although deque also provides Ramdon acces iterator, its iterator is not a common pointer, and its complexity and vector are not an order of magnitude, of course, this will also affect all aspects of the operation. Therefore, unless necessary, we should try to use vector instead of deque. For the highest efficiency, deque can be fully copied to a vector first, and then copied back to deque by using the STL sort algorithm ."

-- The above is taken from Hou Jie's STL source code analysis p143

Deque, meaning double ended Queue (dual-end Queue), which is often read as deck. The method is similar to that of vector, and the interface is basically the same as that of vector. As for the differences between the two and the advantages and disadvantages of the two, the preceding Excerpt from Master Hou's book also gives a clear explanation. The container itself is not absolutely good or bad. The key lies in the application environment and needs.

The member function table is as follows:

Function

Description

Assign (beg, end)

Assign (n, ELEM)

Assign values to the data in the [beg; End) interval.

Assign a copy value to n elem instances.

At (idx)

Returns the data specified by the index idx. If idx is out of bounds, out_of_range is thrown.

Back ()

Returns the last data and does not check whether the data exists.

Begin ()

Returns the unique data of the iterator.

Clear ()

Remove all data from the container

Deque C1 (C2)

Deque C (n)

Deque C (n, ELEM)

Deque C (beg, end)

~ Deque ()

Copy A deque

Create a deque that contains N pieces of data, which are created by default.

Create a deque Containing n elem copies

Create a deque in the [beg; End) Interval

Destroys all data and releases memory.

Empty ()

Judge whether the container is empty

End ()

Point to the last data address in the iterator

Erase (POS)

Erase (beg, end)

Deletes the data at the POs position and returns the location of the next data.

Delete the data in the [beg, end) interval and return the location of the next data.

Front ()

Return a data record

Get_allocator

Returns a copy using the constructor.

Insert (Pos, ELEM)

Insert (Pos, N, ELEM)

Insert (Pos, beg, end)

Insert an ELEM copy at the POs position and return the new data location.

Insert n ELEM data at the POs position, with no returned value

Data inserted in the [beg, end) range at the POs position, no return value

Max_size ()

Maximum number of returned data in the container

Pop_back ()

Delete the last data

Pop_front ()

Delete header data

Push_back (ELEM)

Add data at the end

Push_front (ELEM)

Insert a data entry in the header

Rbegin ()

Returns the first data of a reverse queue.

Rend ()

Returns the next location of the last data in a reverse queue.

Resize (Num)

Specify the queue length again

Size ()

Returns the actual number of data in the container.

C1.swap (C2)

Swap (C1, C2)

Swap C1 and C2 Elements

Same as above

OPERATOR []

Returns a reference from a specified position in the container.

Additional reading:

(1) Today I found a reason why I had to use deque (whether or not I agree with the author's point of view, at least it is helpful to understand deque)

(2) study the STL deque container in C ++ in Depth

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.