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");
}
結果如下