[C ++ STL] the operations of C ++ STL-deque (dual-end Queue)

Source: Internet
Author: User

1) Overview

Deque is a sequence container and a bidirectional open continuous linear space. You can insert and delete elements at the beginning and end respectively. Of course, you can also perform a vector action at both ends of the header and end, but the head action efficiency is very poor. The biggest difference between deque and vector is:Deque allows you to insert or delete elements at both ends of a constant period.; Deque does not have the so-called capacity concept, because it is a combination of continuous space in segments and can be added to join up at any time, there is no such issue as "allocating a larger space due to insufficient space and then copying elements" like a vector.

Sorting Problem: deque provides Random Access iterator, but it is not a native indicator. Its implementation complexity is also great, which also affects the efficiency of related algorithms. If not necessary, use vector instead of deque as much as possible.When sorting, you can copy the elements to a vector before copying them back to deque..

Storage Structure: deque uses a sequential Storage Structure and uses a map (not a map in STL) to manage these segments. Map is a continuous space, and each element is a pointer. Point to another larger continuous linear space and become a buffer zone. The buffer zone is the subject of the deque bucket. Buffer expansion is quite complicated !!!

[Note] deque is a better choice when using the insert/delete operation. It is more appropriate to select vector in other cases.

2) Use

Header file to be loaded: # include <deque>
Using namespace STD;

3) Main Methods

Structure:

Deque <ELEM> C creates an empty deque

Deque <ELEM> C1 (C2) copies a deque.

Deque <ELEM> C (n) creates a deque that contains N pieces of data, which are created by default.

Deque <ELEM> C (n, ELEM) creates a deque Containing n elem copies

Deque <ELEM> C (beg, end) creates a deque in the [beg; End) interval.

C .~ Deque <ELEM> () destroys all data and releases the memory.

Method:

C. Assign (beg, end) assigns the data in the [beg; End) range to C.

C. Assign (n, ELEM) assigns the copy value of n elem to C.

C. At (idx) returns the data referred to by the index idx. If idx is out of bounds, out_of_range is thrown.

C. Back () returns the last data and does not check whether the data exists.

C. Begin ()Returns the first data in the iterator ?????

C. Clear () removes all data from the container.

C. Empty () determines whether the container is empty.

C. End () points to the last data address in the iterator.

C. Erase (POS) deletes the data at the POs position and returns the next data location.

C. Erase (beg, end) deletes data in the [beg, end) interval and returns the location of the next data.

C. Front () returns the first data.

Get_allocator returns a copy using the constructor.

C. insert (Pos, ELEM) inserts an ELEM copy at the POs position and returns the new data location.

C. insert (Pos, N, ELEM) inserts n ELEM data at the POs position. No return value

C. insert (Pos, beg, end) data inserted in the [beg, end) range at the POs position. No return value

C. max_size () returns the maximum number of data in the container.

C. pop_back () Delete the last data.

C. pop_front () deletes the header data.

C. push_back (ELEM) adds a data at the end.

C. push_front (ELEM) inserts a data in the header.

C. rbegin () returns the first data in a reverse queue.

C. rend () returns the next location of the last data in a reverse queue.

C. Resize (Num) re-specifies the queue length.

C. Size () returns the actual number of data in the container.

C. Swap (C2) = swap (C1, C2) Swap C1 and C2 elements.

4) Example

# Include <deque> # include <cstdio> # include <algorithm> using namespace STD; int main () {deque <int> DEQ (20 ); // create a dual-end queue deque with 20 elements <int>: iterator Pos; // iterator int I; for (I = 0; I <20; ++ I) DEQ [I] = I; printf ("output data in deque: \ n"); for (I = 0; I <20; ++ I) printf ("% d", DEQ [I]); putchar ('\ n'); // printf faster than cout ("\ n add new data at the beginning and end... \ n "); DEQ. push_back (100); // insert DEQ at the end. push_front (I); // insert printf ("\ N output data in deque: \ n"); For (Pos = DEQ. Begin (); pos! = DEQ. end (); POS ++) printf ("% d", * POS); putchar ('\ n'); // find const int findnumber = 19; printf ("\ n find % d \ n", findnumber); Pos = find (DEQ. begin (), DEQ. end (), findnumber); // # include <algorithm> If (Pos! = DEQ. end () printf ("find % d success \ n", * POS); elseprintf ("find failed \ n "); printf ("\ n delete data at the beginning and end... \ n "); DEQ. pop_back (); DEQ. pop_front (); printf ("\ n after switching, the data in deque2 is output: \ n"); deque <int> deq2 (20); deq2.swap (DEQ ); for (Pos = deq2.begin (); pos! = Deq2.end (); POS ++) printf ("% d", * POS); putchar ('\ n '); printf ("\ n maximum number of output deque data: \ n"); printf ("% d", DEQ. max_size (); putchar ('\ n'); Return 0 ;}


Puts (char * string)


5) Compare deque and vector

When calling push_back () with a large amount of data, remember to call Vector: Reserve ().

When you allocate a lot of memory units, remember to use deque to recycle the memory more than the time consumed by the vector.

If you plan to use insert () or pop_front (), use deque.

For data access, vector: At () is the most efficient.



 


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.