The first two posts have introduced the vectors and list of the two containers, we found that they each have their own advantages and disadvantages, vector in memory continuous storage, support random access, but the efficiency of the search and delete is relatively low, and list in memory is chained storage is efficient to find and delete, However, random storage is not supported, so deque combines a number of contiguous memory by some means, and looks like it is in the same memory, by overloading the [] operator so that it accesses the data like an array, and the efficiency of the INSERT and delete operations are only affected by the data in the memory segment.
Here's an in-depth review of deque implementation details: http://www.programlife.net/stl-deque.html
1#include <iostream>2#include <deque>3 using namespacestd;4 voidPrint (deque<int>dq)5 {6deque<int>::iterator it=Dq.begin ();7 for(; It!=dq.end (); it++)8 {9cout<<*it<<" ";Ten } Onecout<<Endl; A } - intMain () - { the //Initialize - intnum[Ten]={0,1,2,3,4,5,6,7,8,9}; -deque<int> dq (num,num+Ten); - Print (DQ); + - //add Element + //Insert Adeque<int>PP (num,num+3); at print (PP); -deque<int>::iterator it1=Pp.begin (); -deque<int>::iterator it2=pp.end (); - Dq.insert (Dq.end (), it1,it2); - Print (DQ); - //Push inDq.push_back (Ten); -Dq.push_front (-1); to Print (DQ); + - //Remove deque Only erase, no remove the //Erase *Dq.erase (Dq.begin () +5); $ Print (DQ);Panax Notoginseng - //[] Random access thedq[1]= -; + Print (DQ); A the return 0; +}
Basic operation of C + + STL Deque