Wiki
queue container
About 4 min read
1.queue container
1.1.Basic concepts of queues
Concept:Queue is a kind ofFirst In First Out(First In First Out, FIFO) data structure, it has two exits

A queue container allows adding elements at one end and removing elements from the other end.
Only the front and rear of the queue can be accessed externally; therefore, the queue does not permit traversal behavior.
Inserting data into a queue is called --- enqueue push
Removing data from a queue is called --- dequeue pop
Queues in life:

1.2.Common queue interfaces
Function description: common external interfaces of the stack container
Constructor:
queue<T> que;// The queue is implemented as a template class, and the default constructor form of a queue object isqueue(const queue &que);// copy constructor
Assignment operation:
queue& operator=(const queue &que);// Overload the equality operator
Data Access:
push(elem);// Add an element to the tail of the queuepop();// Remove the first element from the head of the queueback();// Return the last elementfront();// Return the first element
Size operations:
empty();// Determine whether the stack is emptysize();// Return the size of the stack
Example:
#include <iostream>
#include <queue>
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01() {
//创建队列
queue<Person> q;
//准备数据
Person p1("唐僧", 30);
Person p2("孙悟空", 1000);
Person p3("猪八戒", 900);
Person p4("沙僧", 800);
//向队列中添加元素 入队操作
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
//队列不提供迭代器,更不支持随机访问
while (!q.empty()) {
//输出队头元素
cout << "队头元素-- 姓名: " << q.front().m_Name
<< " 年龄: "<< q.front().m_Age << endl;
cout << "队尾元素-- 姓名: " << q.back().m_Name
<< " 年龄: " << q.back().m_Age << endl;
cout << endl;
//弹出队头元素
q.pop();
}
cout << "队列大小为:" << q.size() << endl;
}
int main() {
test01();
return 0;
}
Operation result:
队头元素-- 姓名: 唐僧 年龄: 30
队尾元素-- 姓名: 沙僧 年龄: 800
队头元素-- 姓名: 孙悟空 年龄: 1000
队尾元素-- 姓名: 沙僧 年龄: 800
队头元素-- 姓名: 猪八戒 年龄: 900
队尾元素-- 姓名: 沙僧 年龄: 800
队头元素-- 姓名: 沙僧 年龄: 800
队尾元素-- 姓名: 沙僧 年龄: 800
队列大小为:0
Summary:
- Enqueue --- push
- Dequeue --- pop
- Return the front element --- front
- Return the back element --- back
- Determine whether the queue is empty --- empty
- Return the queue size --- size