Basic operation of C + + STL Deque

Source: Internet
Author: User

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&GT;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

Related Article

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.