1. Introduction to queue
- Queue is a queue container, which is a "first in, first out" container.
- queue simply decorates the deque container and becomes another kind of container.
- #include <queue>
Default construction of queue object
The queue is implemented by template class. The default construction form of the queue object is queue < T > queue
Queue < int > queue; / / a queue container for int.
Queue < float > quefloat; / / a queue container for float.
Queue < string > querystring; / / a queue container for storing strings.
...
//Pointer types or custom types can also be set in angle brackets.
queue Of push()And pop()Method queue.push(elem); //Add elements to the end of the team queue.pop(); //Remove the first element from the team header queue<int> queInt; queInt.push(1);queInt.push(3); queInt.push(5);queInt.push(7); queInt.push(9);queInt.pop(); queInt.pop(); //At this time, the element that queInt stores is 5, 7, 9 queue Copy construction and assignment of objects queue(const queue &que); //copy constructor queue& operator=(const queue &que); //Overloaded equal sign operator queue<int> queIntA; queIntA.push(1); queIntA.push(3); queIntA.push(5); queIntA.push(7); queIntA.push(9); queue<int> queIntB(queIntA); //copy construction queue<int> queIntC; queIntC = queIntA; //assignment
2. Data access of queue
- queue.back(); / / returns the last element
- queue.front(); / / returns 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 queIntA.front() = 11; //11 queIntA.back() = 19; //19
3. The size of the queue
- queue.empty(); / / judge 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 }
Reference: handout of wisdom sweeping monk