C + + STL learning--deque__c++

Source: Internet
Author: User

In the data structure there is also a very common queue called the two-terminal queue, we in the last blog, "C + + STL learning--queue" in the queue is the most standard queue, only at the end of the insertion of data, the head to delete data. And today we talked about the deque can be inserted at both ends of the insertion and deletion, can be said to use more flexible. Sample code uploaded to Https://github.com/chenyufeng1991/STL_deque.

(1) Create a deque

    Deque<int> deque1;
    Deque<int> deque2 (deque1);
    Deque<int> Deque3 (a);
    Deque<int> deque4 (10,0);

(2) deque tail Insert Data

    Deque1.push_back (1);
    Deque1.push_back (2);
    Deque1.push_back (3);
    Deque1.push_back (4);
    Printdeque (deque1);


(3) deque head Insert Data

    Deque1.push_front (5);
    Deque1.push_front (6);
    Printdeque (deque1);

(4) View the head element, tail element, and position element

    cout << "head element is:" << deque1.front () << Endl;
    cout << "tail element is:" << deque1.back () << Endl;

    cout << "The elements of a location are:" << deque1.at (2) << Endl;
    cout << "The elements of a location are:" << deque1[2] << Endl;


(5) Inserts an element at a location using an iterator

Inserting or deleting elements using iterators
    deque<int>::iterator Iterinsert = Deque1.begin ();
    Iterinsert = Iterinsert + 2;
    Deque1.insert (Iterinsert,);
    Printdeque (deque1);


(6) using iterators to delete elements at a location

    Deque<int>::iterator itererase = Deque1.begin ();
    Itererase = Itererase + 2;
    Deque1.erase (itererase);
    Printdeque (deque1);


(7) Size (), max_size ()

    cout << "deque1.size =" << deque1.size () << Endl;
    cout << "deque1.max_size =" << deque1.max_size () << Endl;


(8) head, tail delete element

    Deque1.pop_back ();
    Printdeque (deque1);

    Deque1.pop_front ();
    Printdeque (deque1);


(9) Exchange of two deque

    Exchange of two deque
    deque<int> Dequeswap;
    Dequeswap.push_back (one);
    Dequeswap.push_back ();
    Dequeswap.push_back ();

    cout << "deque1:";
    Printdeque (deque1);
    cout << "Dequeswap:";
    Printdeque (Dequeswap);

    Deque1.swap (Dequeswap);
    cout << "deque1:";
    Printdeque (deque1);
    cout << "Dequeswap:";
    Printdeque (Dequeswap);


(10) To determine whether deque is empty

    cout << "Deque is empty:" << deque1.empty () << Endl;
    Deque1.clear ();
    cout << "Deque is empty:" << deque1.empty () << Endl;
    Printdeque (deque1);


(11) Iterator print Deque

void Printdeque (deque<int> pdeque)
{
    cout << "deque element is:";
    Deque<int>::iterator Dequeiter;
    for (Dequeiter = Pdeque.begin (); Dequeiter!= pdeque.end (); dequeiter++)
    {
        cout << *dequeiter << ""; c6/>}

    cout << Endl;
}

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.