是否可以在 C++ 中遍历 std::stack
?
使用以下方法遍历不适用。因为 std::stack
没有成员 end
。
std::stack<int> foo;
// ..
for (__typeof(foo.begin()) it = foo.begin(); it != foo.end(); it++)
{
// ...
}
最佳答案
Is it possible to traverse std::stack in C++?
没有。当您有兴趣将元素放在顶部并从顶部获取元素时,堆栈是您应该使用的数据结构。如果您想要一个可迭代的堆栈,请为堆栈角色使用不同的数据结构(std::vector
?)或自己编写一个。
关于c++ - C++中如何遍历栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23194548/