STL Series 5: Queue container and stl series queue container

Source: Internet
Author: User

STL Series 5: Queue container and stl series queue container
Queue Introduction

  • Queue is a queue container and a "first-in-first-out" container.
  • Queue is a simple deque container and becomes another container.
  • # Include <queue>

1. Default Construction of the queue object

The queue is implemented using the template class. The default construction form of the queue object is queue <T> queT; for example, queue <int> queInt; // a queue container that stores the int. Queue <float> queFloat; // a queue container that stores float .... // You can also set the pointer type or custom type in the angle brackets.
2. The push () and pop () Methods of queue
  • Queue. push (elem); // add elements to the end of the team
  • Queue. pop (); // remove the first element from the queue Header
# Include <iostream> using namespace std; # include <queue> void objPlay2 () {queue <int> queInt; queInt. push (1); queInt. push (3); queInt. push (5); queInt. push (7); queInt. push (9); queInt. pop (); queInt. pop (); // The elements stored in the queInt are 5, 7, 9} int main () {objPlay2 (); return 0 ;}

 

3. Copy construction and assignment of the queue object
  • Queue (const queue & que); // copy the constructor
  • Queue & operator = (const queue & que); // overload the equal sign operator
Void objPlay3 () {queue <int> queIntA; queIntA. push (1); queIntA. push (3); queIntA. push (5); queIntA. push (7); queIntA. push (9); queue <int> queIntB (queIntA); // copy and construct the queue <int> queIntC; queIntC = queIntA; // assign value}
4. Data Access to queue
  • Queue. back (); // returns the last element.
  • Queue. front (); // returns the first element.
Void objPlay4 () {queue <int> queIntA; queIntA. push (1); queIntA. push (3); queIntA. push (5); queIntA. push (7); queIntA. push (9); int iFront = queIntA. front (); // get the queue Header element, 1 int iBack = queIntA. back (); // get the end element of the queue, 9 queIntA. front () = 11; // 11 queIntA. back () = 19; // 19}
5. queue size
  • Queue. empty (); // determines whether the queue is empty.
  • Queue. size (); // the size of the returned queue
    Void objPlay5 () {queue <int> queIntA; queIntA. push (1); queIntA. push (3); queIntA. push (5); queIntA. push (7); queIntA. push (9); if (! QueIntA. empty () {int iSize = queIntA. size (); // There are five elements in the queue }}

     

 Sort all the above Code:

# Include <iostream> using namespace std; # include <queue> void objPlay2 () {queue <int> queInt; queInt. push (1); queInt. push (3); queInt. push (5); queInt. push (7); queInt. push (9); queInt. pop (); queInt. pop (); // The elements stored in the queInt are 5, 7, 9} void objPlay3 () {queue <int> queIntA; queIntA. push (1); queIntA. push (3); queIntA. push (5); queIntA. push (7); queIntA. push (9); queue <int> queIntB (queIntA); // copy and construct the queue <int> queIntC; qu EIntC = queIntA; // value assignment} void objPlay4 () {queue <int> queIntA; queIntA. push (1); queIntA. push (3); queIntA. push (5); queIntA. push (7); queIntA. push (9); int iFront = queIntA. front (); // get the queue Header element, 1 int iBack = queIntA. back (); // get the end element of the queue, 9 queIntA. front () = 11; // 11 queIntA. back () = 19; // 19} void objPlay5 () {queue <int> queIntA; queIntA. push (1); queIntA. push (3); queIntA. push (5); queIntA. push (7); queIntA. push (9); if (! QueIntA. empty () {int iSize = queIntA. size (); // Five Elements in the queue} int main () {objPlay2 (); objPlay3 (); objPlay4 (); objPlay5 (); return 0 ;}

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.