隊列容器默認實現爲雙端隊列容器,用戶也可以指定使用鏈表容器來實現,但不能使用向量容器,否則會編譯出錯。
這是因爲隊列容器裏面的出隊列pop()是通過調用pop_front()來實現的,pop_front()爲底層容器的成員函數,但向量容器不包含這樣的成員。
#include <queue>
queue<int> q1; //默認爲雙端隊列
queue<int, list<int> > q2; //鏈表 兩個>之間要有空格
構造:
#include <queue>
queue();
queue( const Container& con );
成員函數:
back |
returns a reference to last element of a queue |
empty |
true if the queue has no elements |
front |
returns a reference to the first element of a queue |
pop |
removes the top element of a queue |
push |
adds an element to the end of the queue |
size |
returns the number of items in the queue |
中文:
成員函數 |
實現操作 |
T& back() |
返回隊列最後一個元素 |
const T& back()const |
返回隊列最後一個元素 |
bool empty()const |
如果隊列爲空,返回true,否則返回爲false |
T& front() |
返回隊列第一個元素 |
const T& front()const |
返回隊列第一個元素 |
void pop() |
移去隊列中的第一個元素 |
void push(const T& el) |
在隊列尾部插入元素el |
queue() |
創建一個空隊列 |
size_type size()const |
返回隊列中元素數目 |
注意:出隊列時要先調用front()取得第一個元素,再調用pop().