C ++ std :: stack遍历 [英] C++ std::stack Traversal
问题描述
我使用 std :: stack
为一个项目,我需要旅行,以检查相同的值。我检查了成员函数,但是我找不到一个合适的任务。
第一个想法是使用一个副本堆栈,但程序可能浪费很多这种情况下的额外空间,而不是使用用户定义的堆栈类在项目的这一级很重要(是的,我犯了一个设计错误...)。
避免 std :: stack
,它只是一个无用的包装器,淹没了底层容器的接口。使用 std :: vector
与 push_back
/ pop_back
插入插入/删除(插入/删除在末尾被摊销O(1))或 std :: deque
,因此你可以推/弹出没有重大更改表现(仍然摊销O(1))。在这两种情况下,你可以使用他们的随机访问迭代器来遍历所有的元素。
( std :: queue
:无用,直接使用 push_back $ c $>直接使用
std :: deque
( not < c> / pop_front
)
I am using std::stack
for an project and I need to travel on it for checking same values. I checked member functions but, I could not find an appropriate one for this task.
The first idea that came up is using a copy stack but, the program might waste lots of extra space in this case, and not using an user-defined stack class is important at this level of the project (Yeah, I made a design mistake... ).
So, any idea?
Thank you!
Avoid std::stack
, it's just a useless wrapper that dumbs down the interface of the underlying container. Use a std::vector
with push_back
/pop_back
for insert insertion/removal (insertion/removal at the end is amortized O(1)) or std::deque
, whence you can push/pop on either side without significant changes in performances (still amortized O(1)). In both cases you can walk all the elements using their random-access iterators.
(the same holds for std::queue
: it's useless, use directly std::deque
(not vector) with push_back
/pop_front
)
这篇关于C ++ std :: stack遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!