STL_queue container

1. Introduction to queue

The entry and exit of all elements of the queue must meet the "first in, first out" condition, and only the top element of the queue has the opportunity to be used by the outside world. Queue does not provide traversal functions, nor does it provide iterators.

Queue is simply decorated deque container to become another kind of container.

#include <queue>

Insert picture description here

Second, the default structure of the queue object

Queue is implemented using template classes, and the default structure of the queue object: queue<T> queT;

queue<int> queInt;      //一个存放int的queue容器。
queue<float> queFloat;   //一个存放float的queue容器。
queue<string> queString;   //一个存放string的queue容器。
//尖括号内还可以设置指针类型或自定义类型。          

Three, the push() and pop() methods of queue

queue.push(elem); //Add elements to the end of the queue

queue.pop(); //Remove the first element from the head of the queue

queue<int> queInt;

queInt.push(1);
queInt.push(3);
queInt.push(5);
queInt.push(7);
queInt.push(9);
queInt.pop();
queInt.pop();
//此时queInt存放的元素是5,7,9

Fourth, the copy construction and assignment of the queue object

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

queue& operator=(const queue &que); //Overload the equal sign operator

queue<int> queIntA;
queIntA.push(1);
queIntA.push(3); 
queue<int> queIntB(queIntA);    //拷贝构造
queue<int> queIntC;
queIntC = queIntA;              //赋值

Five, queue data access

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

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

queue<int> 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

Six, the size of the queue

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

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

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();     //5
}

Guess you like

Origin blog.csdn.net/weixin_45341339/article/details/113126824