队列容器默认实现为双端队列容器,用户也可以指定使用链表容器来实现,但不能使用向量容器,否则会编译出错。
这是因为队列容器里面的出队列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().