C++ queue(队列)提供了队列的全部功能,换句话说就是这里面已经实现了一个先进先出的数据结构。不需要我们再去重新定义各种函数,简化开发过程。 c++ 队列queue的头文件书写格式为:
#include <queue>实例化形式如下:
queue<ElemType> QueueName;其中成员函数如下:
1、检验队列是否为空
empty() 堆栈为空则返回真 形式如下:
QueueName.empty();2、返回队首元素 front() 返回队首元素 形式如下:
QueueName.front();3、返回队尾元素 back()返回队尾元素 形式如下:
QueueName.back() ;4、弹出队首元素 pop() 移除队列中最靠前位置的元素,是没有返回值的void函数 形式如下:
QueueName.pop();5、插入元素 push() 在队尾插入一个元素 形式如下:
QueueName.push(ElemType);6、栈中数据的数量 size() 返回队列中元素数目 形式如下:
QueueName.size();例子如下
#include <iostream> #include <queue> #include <stdlib.h> using namespace std; int main() { queue<int> Queue; Queue.push(1); Queue.push(2); Queue.push(3); cout << "the front of the queue is:" << Queue.front() << endl; cout << "the size of the queue is:" << Queue.size() << endl; cout << "whether the queue is empty(1:yes 0:not):" << Queue.empty() << endl; Queue.pop(); cout << "the front of the queue is:" << Queue.front() << endl; cout << "the back of the queue is:" << Queue.back() << endl; Queue.pop(); cout << "the front of the queue is:" << Queue.front() << endl; Queue.pop(); cout << "whether the queue is empty(1:yes 0:not):" << Queue.empty() << endl; system("pause"); }结果如下