包含头文件<queue>
queue<int> qData; //创建一个队列
队列的处理函数、操作和栈基本一样
先到先服务,先放入的数据先拿出来
队列可以用来遍历二叉树(层次遍历):数据结构 --- c语言非递归法遍历二叉树_小雪菜本菜的博客-CSDN博客_c语言非递归遍历
#include <queue>
#include <iostream>
#include <string>
using namespace std;
//不影响原容器
void pop_queue(queue<int> qData)
{
while (!qData.empty())
{
cout << qData.front() << " "; //获取队头元素
qData.pop(); //出队
}
cout << endl;
}
void testQueue()
{
queue<int> qData;
for (int i = 0; i < 3; i++)
{
qData.push(i); //入队 0 1 2
}
while (!qData.empty())
{
cout << qData.front() << " "; //获取队头元素
qData.pop(); //出队
}
cout << endl;
cout << qData.size() << endl;
}
int main()
{
testQueue();
return 0;
}
/* 输出 */
0 1 2
0