STL_ Queue container

Time:2021-4-15

1、 Introduction to queue

All elements in and out of the queue must meet the “first in, first out” condition. Only the top element of the queue can be accessed by the outside world. The queue does not provide traversal function or iterator.

Queue simply decorates deque container and becomes another kind of container.

#include

2、 The default construction of the queue object

Queue is implemented by template class, and the default construction form of queue object is queue queT;

Queue queueint; // a queue container for holding ints.
Queue queue float; // a queue container for storing floats.
Queue queue string; // a queue container for storing strings.
//You can also set pointer type or custom type in angle brackets.

3、 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 team head

queue queInt;

queInt.push(1);
queInt.push(3);
queInt.push(5);
queInt.push(7);
queInt.push(9);
queInt.pop();
queInt.pop();
//At this time, the elements of queint are 5, 7 and 9

4、 Copy construction and assignment of queue object

Queue (const queue & que); / / copy constructor

Queue & operator = (const queue & que); / / overload equal sign operator

queue queIntA;
queIntA.push(1);
queIntA.push(3); 
Queue queueintb (queueinta); // copy construction
queue queIntC;
Queintc = queinta; // assignment

5、 Data access of queue

queue.back (); / / returns the last element

queue.front (); / / returns the first element

queue queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
int iFront = queIntA.front();       //1
int iBack = queIntA.back();       //9

6、 The size of the queue

queue.empty (); / / judge whether the queue is empty

queue.size (); / / returns the size of the queue

queue queIntA;    
queIntA.push(1);      
queIntA.push(3);      
queIntA.push(5);       
queIntA.push(7);       
queIntA.push(9);       
if (!queIntA.empty()){
   int iSize = queIntA.size();     //5
}

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]