STL_queue
queue的基本概念:
一般的queue容器只能队尾进,队首出,双向队列deque那就是另一回事儿了。
queue常用接口
一段代码学会队列的使用:
#include<iostream> #include<queue> using namespace std; // 队列 Queue class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; void test01() { // Create queue // 创建一个队列 queue<Person>q; // Data preparation // 准备数据 Person p1("唐僧", 30); Person p2("孙悟空", 1000); Person p3("猪八戒", 900); Person p4("沙僧", 800); // Push // 往队列里面添加元素 q.push(p1); q.push(p2); q.push(p3); q.push(p4); cout << "队列大小为:" << q.size() << endl; // Judge whether the queue is empty or not, check the opposite head, check the end of the queue, and exit the queue // 判断队列是否为空,然后检查对首和队尾,最后退出队列 while (!q.empty()) { // check the opposite head // 查看对首元素 cout << "对头元素 --- 姓名:" << q.front().m_Name << "年龄:" << q.front().m_Age << endl; // check the end of the queue // 查看队尾元素 cout << "队尾元素 --- 姓名:" << q.back().m_Name << "年龄:" << q.front().m_Age << endl; cout << endl; // 出队列方法 // out of queue q.pop(); } cout << "队列大小为:" << q.size() << endl; } int main() { test01(); return 0; }
运行结果: