说明:C++中的队列queue自身不支持clear操作,但双端队列deque是支持clear操作的。
方法一:直接用空的队列对象赋值
queue<int> q1;
//process
//……
q1=queue<int>();
方法二:遍历出队列
while(!Q.empty())
{
Q.pop();
}
方法三:使用swap实现
使用swap定义clear,保持STL容器的标准,这种是最高效的
void clear(queue<int>& q)
{
queue<int> empty;
swap(empty,q);
}
参考博客:https://www.cnblogs.com/zhonghuasong/p/7524624.html