有个破坏性的方法。
因为stack不支持迭代器,所以可以
int val;
while(!container.empty())
{
val=container.top();
container.pop();
}
这么遍历。
缺点是读一次之后里面的元素都没了。
不过可以这样,开多一个stack,边读的时候边入栈,然后再读回原来的stack
int val;
stack <int> container;
stack <int> temp;
while(!container.empty())
{
val=container.top();
_queue.push(val);
container.pop();
}
//复原
while(!temp.empty())
{
val=temp.top();
temp.pop();
container.push(val);
}
这样大概可以了,没试过,要是出错了可以留言。