如何在 C++ 中遍历堆栈? [英] How to traverse stack in C++?

查看:104
本文介绍了如何在 C++ 中遍历堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C++中是否可以遍历std::stack?

Is it possible to traverse std::stack in C++?

使用以下方法遍历不适用.因为 std::stack 没有成员 end.

Traversing using following method is not applicable. Because std::stack has no member end.

std::stack<int> foo;

// ..

for (__typeof(foo.begin()) it = foo.begin(); it != foo.end();  it++)
{
    // ...
}

推荐答案

是否可以在 C++ 中遍历 std::stack?

Is it possible to traverse std::stack in C++?

没有.当您有兴趣将元素放置在顶部并从顶部获取元素时,堆栈是一种您应该使用的数据结构.如果您想要一个可迭代的堆栈,请为堆栈角色使用不同的数据结构 (std::vector?) 或自己编写一个.

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.

这篇关于如何在 C++ 中遍历堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆