Sign in to follow this  

Removing all elements in a std::stack

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There doesn't seem to be a clear() method or anything. I guess i could iterate over it and pop() each element but that seems kinda stupid since that could be a simple method in std::stack.

Share this post


Link to post
Share on other sites
Just assign an empty stack:

int main()
{
std::stack<int> s;
s.push(2);
s.push(3);
s.push(5);
s = std::stack<int>();
}

If you want to be more explicit, write a function template:

template<typename T>
void clear_stack(std::stack<T>& s)
{
s = std::stack<T>();
}

int main()
{
std::stack<int> s;
s.push(2);
s.push(3);
s.push(5);
clear_stack(s);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Just assign an empty stack

Good idea. thanks

But what about iterating over a stack. See, i use a stack to hold all visible GUI components in my game. So i need to reverse iterate over them to draw them back-to-front.

Share this post


Link to post
Share on other sites
Quote:
Original post by XTAL256

But what about iterating over a stack. See, i use a stack to hold all visible GUI components in my game. So i need to reverse iterate over them to draw them back-to-front.


Then stack is the wrong choice. Stack is FILO. deque can FIFO or FILO, and it supports random access.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Just assign an empty stack:

int main()
{
std::stack<int> s;
s.push(2);
s.push(3);
s.push(5);
s = std::stack<int>();
}

If you want to be more explicit, write a function template:

template<typename T>
void clear_stack(std::stack<T>& s)
{
s = std::stack<T>();
}

int main()
{
std::stack<int> s;
s.push(2);
s.push(3);
s.push(5);
clear_stack(s);
}



Alternatively, if the stack-items need to be explicitly ~destructed/deleted/deleted[]/free()'d, you might want to loop over all elements and while looping ~destruct/delete/delete[]/free() every item.


while (!stack.empty()) {
// cleanup stack.top()
stack.pop();
}

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
Alternatively, if the stack-items need to be explicitly ~destructed/deleted/deleted[]/free()'d, you might want to loop over all elements and while looping ~destruct/delete/delete[]/free() every item.


while (!stack.empty()) {
// cleanup stack.top()
stack.pop();
}


You could do that, but the destructor of the stack will do it too. And it's extremely unlikely that you're supposed to call the destructor of the elements manually.

If you need to call 'delete' on the stack-items, then (a) you have a design problem (use smart pointers, or at least do RAII yourself), and (b) that's not really cleaning up the stack item, but the thing that the stack item points at.

Quote:
But what about iterating over a stack. See, i use a stack to hold all visible GUI components in my game. So i need to reverse iterate over them to draw them back-to-front.


'stack' is not a container in itself; it's a wrapper for a container that deliberately hides all the elements from you beside the top. It's meant to allow you to declare that you're only interested in the top element at any given time.

Since this is apparently not in fact the case, you should not be using it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by phresnel
Alternatively, if the stack-items need to be explicitly ~destructed/deleted/deleted[]/free()'d, you might want to loop over all elements and while looping ~destruct/delete/delete[]/free() every item.


while (!stack.empty()) {
// cleanup stack.top()
stack.pop();
}


You could do that, but the destructor of the stack will do it too. And it's extremely unlikely that you're supposed to call the destructor of the elements manually.

If you need to call 'delete' on the stack-items, then (a) you have a design problem (use smart pointers, or at least do RAII yourself), and (b) that's not really cleaning up the stack item, but the thing that the stack item points at.


Indeed I was posting about extreme corner cases (e.g. where you are at the very very late late stage of performance-optimisation for the last few percent, and you as the wizard just detected that you could safe 10 cycles by not using a smart pointer) and I should have added a disclaimer. Mea Culpa. (Sidenote: I am an advocate of RAII and not using blank pointers in general, too: picogen-styleguide on RAII (disclaimer: that styleguide was written in a hurry and shall currently not be used by anyone)).

Share this post


Link to post
Share on other sites

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this