Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to traverse stack in C++?

Tags:

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

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++) {     // ... } 
like image 711
user1857492 Avatar asked Apr 21 '14 09:04

user1857492


1 Answers

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

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.

like image 163
utnapistim Avatar answered Dec 20 '22 20:12

utnapistim