Deque bidirectional queue of STL

Source: Internet
Author: User
Tags first string

Deque bidirectional queue is a two-way open continuous linear space that can efficiently insert and delete elements at both ends of the head and end to provide random access. deque is very similar to vector on the interface, the following lists common deque member functions:

Table 6.9. constructors and destructor of deques

Operation Effect
Deque <ELEM> C Creates an empty deque without any elements
Deque <ELEM> C1 (C2) Creates a copy of another deque of the same type (all elements are copied)
Deque <ELEM> C (n) Creates a dequeNElements that are created by the default constructor
Deque <ELEM> C (n, ELEM) Creates a deque initializedNCopies of ElementELEM
Deque <ELEM> C (beg, end) Creates a deque initialized with the elements of the range[Beg, end)
C .~ Deque <ELEM> () Destroys all elements and frees the memory

 

Table 6.10. nonmodifying operations of deques

Operation Effect
C. Size () Returns the actualNumber of Elements
C. Empty () Returns whether the container is empty (equivalentSize ()= 0,But might be faster)
C. max_size () Returns the maximum number of elements possible
C1 = c2 Returns whetherC1Is equalC2
C1! = C2 Returns whetherC1Is not equalC2(Equivalent! (C1 = c2))
C1 <C2 Returns whetherC1Is lessC2
C1> C2 Returns whetherC1Is greaterC2(EquivalentC2 <C1)
C1 <= c2 Returns whetherC1Is less than or equalC2(Equivalent! (C2 <C1))
C1> = c2 Returns whetherC1Is greater than or equalC2(Equivalent! (C1 <C2))
C. At (idx) Returns the element with indexIdx(Throws range error exception ifIdxIs out of range)
C [idx] Returns the element with indexIdx(NoRange checking)
C. Front () Returns the first element (NoCheck whether a first element exists)
C. Back () Returns the last element (no check whether a last element exists)
C. Begin () Returns a random access iterator for the first element
C. End () Returns a random access iterator for the position after the last element
C. rbegin () Returns a reverse iterator for the first element of a reverse Iteration
C. rend () Returns a reverse iterator for the position after the last element of a reverse Iteration

Table 6.11. Modifying operations of deques

Operation Effect
C1 = c2 Assigns all elementsC2ToC1
C. Assign (n, ELEM) Assigns n copies of ElementELEM
C. Assign (beg, end) Assigns the elements of the range[Beg, end)
C1.swap (C2) Swaps the dataC1AndC2
Swap (C1, C2) Same (as global function)
C. insert (Pos, ELEM) Inserts at iterator positionPosA copyELEMAnd returns the position of the new element
C. insert (Pos, N, ELEM) Inserts at iterator positionPos nCopiesELEM(Returns nothing)
C. insert (Pos, beg, end) Inserts at iterator positionPosA copy of all elements of the range[Beg, end) (Returns nothing)
C. push_back (ELEM) Appends a copyELEMAt the end
C. pop_back () Removes the last element (does not return it)
C. push_front (ELEM) Inserts a copyELEMAt the beginning
C. pop_front () Removes the first element (does not return it)
C. Erase (POS) Removes the element at iterator positionPosAnd returns the position of the next element
C. Erase (beg, end) Removes all elements of the range[Beg, end) And returns the position of the next element
C. Resize (Num) Changes the number of elementsNum(IfSize ()Grows, new elements are created by their default constructor)
C. Resize (Num, ELEM) Changes the number of elements to num (ifSize ()Grows, new elements are copiesELEM)
C. Clear () Removes all elements (makes the container empty)

 

The deque implementation is complicated and a map is maintained internally (note! Is not a map container in STL)That is, a small continuous space, In this spaceEach element is a pointer., Pointing to another (larger) area, which is called a buffer, which is used to save data in deque. Therefore, deque is slower than vector in random access and data traversal. For specific deque implementation, refer to STL source code analysis. Of course, the implementation methods of sgi stl used in this book are different from those of pj stl used in vs2008. The structure of deque is given below:

Due to space limitations, the Implementation Details of deque are no longer in-depth. The following is an example of deque:

// cont/deque1. cpp   #include <iostream>   #include <deque>   #include <string>   #include <algorithm>   using namespace std;   int main()   {       //create empty deque of strings       deque<string> coll;       //insert several elements       coll.assign (3, string("string"));       coll.push_back ("last string");       coll.push_front ("first string");       //print elements separated by newlines       copy (coll.begin(), coll.end(),             ostream_iterator<string>(cout,"\n"));       cout << endl;       //remove first and last element       coll.pop_front();       coll.pop_back();       //insert ‘‘another‘‘ into every element but the first       for (int i=1; i<coll.size(); ++i) {           coll[i] = "another " + coll [i];       }       //change size to four elements       coll.resize (4, "resized string");       //print elements separated by newlines       copy (coll.begin(), coll.end(),             ostream_iterator<string>(cout,"\n"));   }

The program has the following output:

   first string   string   string   string   last string   string   another string   another string   resized string

Deque bidirectional queue of STL

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.