C++ 如何在C++中遍历堆栈?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23194548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to traverse stack in C++?
提问by user1857492
Is it possible to traverse std::stack
in C++?
是否可以std::stack
在 C++ 中遍历?
Traversing using following method is not applicable. Because std::stack
has no member end
.
使用以下方法遍历不适用。因为std::stack
没有会员end
。
std::stack<int> foo;
// ..
for (__typeof(foo.begin()) it = foo.begin(); it != foo.end(); it++)
{
// ...
}
回答by utnapistim
Is it possible to traverse std::stack in C++?
是否可以在 C++ 中遍历 std::stack?
No. A stack is a data structure you should use when you are interested in placing elements on top and getting elements from the top. If you want an iterable stack, either use a different data structure for a stack role (std::vector
?) or write one yourself.
不。当您有兴趣将元素放置在顶部并从顶部获取元素时,应该使用堆栈是一种数据结构。如果您想要一个可迭代的堆栈,请为堆栈角色使用不同的数据结构 ( std::vector
?) 或自己编写一个。
回答by Arun Suryan
It is not possible to directly traverse an std:: stack
as it does not have an end
member and that's how a stack data-structure is supposed to be i.e. only have one pointer. But, still here are two lazy hacks to traverse it:
不可能直接遍历 an,std:: stack
因为它没有end
成员,这就是堆栈数据结构应该如何,即只有一个指针。但是,仍然有两个懒惰的技巧可以遍历它:
1) Loop Based:
1) 基于循环:
while(!st.empty()) {
cout << s.top();
s.pop();
}
Problems with the loop-based approach:
基于循环的方法的问题:
- The original stack gets empty.
- 原始堆栈变空。
2) Recursion Based:
2) 基于递归:
template <typename T>
void traverse_stack(stack<T> & st) {
if(st.empty())
return;
T x = st.top();
cout << x << " ";
st.pop();
traverse_stack(st);
st.push(x);
}
Advantages of Recursion based approach:
基于递归的方法的优点:
- Maintains the original stack elements.
- 保持原始堆栈元素。
Problems with Recursion based approach:
基于递归的方法的问题:
- Maintains an internal stack.
- May fail for large size of the stack.
- 维护一个内部堆栈。
- 可能会因堆栈较大而失败。
回答by A. Knorre
As you mentioned you need printing for debugging purposes, maybe something like this would work for you:
正如您提到的,您需要出于调试目的进行打印,也许这样的事情对您有用:
// Example program
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>
template <typename T>
void StackDebug(std::stack<T> s)
{
std::vector<T> debugVector = std::vector<T>();
while (!s.empty( ) )
{
T t = s.top( );
debugVector.push_back(t);
s.pop( );
}
// stack, read from top down, is reversed relative to its creation (from bot to top)
std::reverse(debugVector.begin(), debugVector.end());
for(const auto& it : debugVector)
{
std::cout << it << " ";
}
}
int main()
{
std::stack< int > numbers;
numbers.push( 9 );
numbers.push( 11 );
StackDebug(numbers);
}
Output is, as expected, "9 11"
正如预期的那样,输出是“9 11”
回答by Rahul Tripathi
I don'tthink that it is possible to traverse through a stack. The best I can think of is using vector using std::vector
using push_back(), pop_back()
我不认为这是有可能通过遍历堆栈。我能想到的最好的方法是使用 vector 使用std::vector
usingpush_back(), pop_back()
The stack does not provide a begin or end member function so you cannot use it with a range based for loopwhich requires both.
堆栈不提供 begin 或 end 成员函数,因此您不能将它与需要两者的基于范围的 for 循环一起使用。
In your case it would be better to choose some other data structure if you really want to iterate through it.
在您的情况下,如果您真的想遍历它,最好选择其他一些数据结构。
回答by DNamto
We can't traverse through stack. Stacks are a type of container adaptor, specifically designed to operate in a LIFOcontext (last-in first-out), where elements are inserted and extracted only from one end of the container. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack. It is not intended for stack to show this behavior, for this we have other containers
我们不能遍历堆栈。堆栈是一种容器适配器,专门设计用于在LIFO上下文(后进先出)中操作,其中元素仅从容器的一端插入和提取。元素从特定容器的“后部”(称为堆栈的顶部)推送/弹出。堆栈不打算显示此行为,为此我们有其他容器
回答by Ben Crowhurst
#include <stack>
using std::stack;
stack< int > numbers;
numbers.push( 1 );
numbers.push( 2 );
while ( not numbers.empty( ) )
{
int number = numbers.top( );
numbers.pop( );
}
回答by Shaun Wang
You can do a for loop:
你可以做一个 for 循环:
for (stack<T> newStack = stack; !newStack.empty(); newStack.pop()){
T item = newStack.top();
}