Removing all elements in a std::stack

Started by
6 comments, last by phresnel 14 years, 10 months ago
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.
[Window Detective] - Windows UI spy utility for programmers
Advertisement
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);}
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.
[Window Detective] - Windows UI spy utility for programmers
Quote:Original post by XTAL256
i use a stack to hold all visible GUI components in my game.

Why? Just use a vector.
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.
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();    }
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.
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)).

This topic is closed to new replies.

Advertisement