C++ queue stack 清空操作
没有 q.clear()
这种操作啊
一个一个 pop
出来肯定是可以的
还可以直接给对象赋空值
最高效的方法就是和空队列/栈进行 swap
:
//queue void clearQueue1(queue<elemType> & q) { queue<elemType> empty; swap(empty, q); //or: //queue<elemType>().swap(q); } void clearQueue2(queue<elemType> & q) { q = queue<int>(); } //stack void clearStack1(stack<elemType> &s){ s = stack<elemType>(); } void clearStack2(stack<elemType> &s){ stack<elemType>().swap(s); }
本文作者:Ryomk
本文链接:https://www.cnblogs.com/preccrep/p/16477591.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。