How to walk through a Stack without destroying it?

A

Andrew

Since stack does not provide iterator interfaces, how to iterate
over it (e.g., print out each element in it for debug purpose) without
changing its structure or copying the whole stack to another stack for such
purpose?

Thanks a lot.

-Andrew
 
P

Pete Becker

Andrew said:
Since stack does not provide iterator interfaces, how to iterate
over it (e.g., print out each element in it for debug purpose) without
changing its structure or copying the whole stack to another stack for such
purpose?

If you need operations that aren't supported by a stack don't use a
stack. vector or deque might be what you need.
 
J

Jonathan Turkanis

Andrew said:
Since stack does not provide iterator interfaces, how to iterate
over it (e.g., print out each element in it for debug purpose) without
changing its structure or copying the whole stack to another stack for such
purpose?

A stack is just a thin wrapper around another container. The point of
using a stack is to make your code self-documentating --to emphasize
that you don't need to use the other operations supported by the
underlying container. So if this last condition is not satisfied,
there's no reason to use a stack.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top