目录
queue类介绍
queue类定义
queue类构造函数
queue类数据操作
empty()函数
size()函数
front()函数
back()函数
push()函数
pop()函数
swap()函数
queue类介绍
- 队列是一种容器适配器,专门用于在FIFO上下文(先进先出)中操作,其中从容器一端插入元素,另一端提取元素。
- 队列作为容器适配器实现,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从队尾入队列,从队头出队列。
- 底层容器可以是标准容器类模板之一,也可以是其他专门设计的容器类。
- 标准容器类deque和list满足了这些要求。默认情况下,如果没有为queue实例化指定容器类,则使用标准容器deque。
queue类定义
template <class T, class Container = deque<T> > class queue;queue类为类模板,所以在使用时需要带上类型表示一个具体的类,例如数据类型为int类型的queue使用时需要写为queue<int>
queue类构造函数
| 构造函数 | 函数原型 | 
| 无参构造函数 | 
 | 
📌
上面表格中的构造函数均含有自定义空间配置器并带有缺省值,目前只使用默认即可
📌
使用queue类需要包含头文件<queue>
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);q.push(5);//打印队列while (!q.empty()){cout << q.front() << " ";q.pop();}cout << endl;return 0;
}
输出结果:
1 2 3 4 5queue类数据操作
| 函数 | 功能 | 
| 
 | 判断调用对象队列是否为空 | 
| 
 | 获取调用对象队列有效数据个数 | 
| 
 | 获取调用对象队列对头数据 | 
| 
 | 获取调用对象队列队尾数据 | 
| 
 | 向调用对象队列尾部插入数据 | 
| 
 | 弹出调用对象队列对头元素 | 
| 
 | 调用对象队列与指定对象队列交换 | 
empty()函数
 
使用empty()函数可以判断调用对象队列是否为空
| 函数 | 函数原型 | 
| 
 | 
 | 
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);q.push(5);cout << q.empty() << endl;return 0;
}
输出结果:
0size()函数
 
使用size()函数可以获取调用对象队列有效数据个数
| 函数 | 函数原型 | 
| 
 | 
 | 
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);q.push(5);cout << q.size() << endl;return 0;
}
输出结果:
5front()函数
 
使用front()函数可以获取调用对象队列对头元素
| 函数 | 函数原型 | 
| 
 | 
 | 
| 
 | 
📌
如果队列为空,此时调用front()函数将会断言报错
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);q.push(5);cout << q.front() << endl;return 0;
}
输出结果:
1back()函数
 
使用back()函数可以获取调用对象队列队尾数据
| 函数 | 函数原型 | 
| 
 | 
 | 
| 
 | 
📌
如果调用对象队列为空,调用back()函数将会断言报错
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);q.push(5);cout << q.back() << endl;return 0;
}
输出结果:
5push()函数
 
使用push()函数可以向调用对象队列中插入数据
| 函数 | 函数原型 | 
| 
 | 
 | 
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);q.push(5);//打印队列while (!q.empty()){cout << q.front() << " ";q.pop();}cout << endl;return 0;
}
输出结果:
1 2 3 4 5pop()函数
 
使用pop()函数可以弹出调用对象队列对头数据
| 函数 | 函数原型 | 
| 
 | 
 | 
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);q.push(5);//打印队列while (!q.empty()){cout << q.front() << " ";q.pop();}cout << endl;return 0;
}
输出结果:
1 2 3 4 5swap()函数
 
使用swap()函数可以交换调用对象队列和指定对象队列
| 函数 | 函数原型 | 
| 
 | 
 | 
示例代码:
#include <iostream>
#include <queue>
using namespace std;int main()
{queue<int> q;queue<int> q1;q.push(1);q.push(1);q.push(1);q.push(1);q.push(1);q1.push(2);q1.push(2);q1.push(2);q1.push(2);q1.push(2);cout << "交换前:" << endl;//打印栈while (!q.empty()){cout << q.front() << " ";q.pop();}cout << endl;//打印栈while (!q1.empty()){cout << q1.front() << " ";q1.pop();}cout << endl;// 注意打印已经使栈为空,需要重新插入元素q.push(1);q.push(1);q.push(1);q.push(1);q.push(1);q1.push(2);q1.push(2);q1.push(2);q1.push(2);q1.push(2);q.swap(q1);cout << "交换后:" << endl;//打印栈while (!q.empty()){cout << q.front() << " ";q.pop();}cout << endl;//打印栈while (!q1.empty()){cout << q1.front() << " ";q1.pop();}cout << endl;return 0;
}
输出结果:
交换前:
1 1 1 1 1
2 2 2 2 2
交换后:
2 2 2 2 2
1 1 1 1 1