Misbah Ahmad Misbah Ahmad - 3 months ago 22
C++ Question

how can i clear a stack in c++ efficiently?

I have a c++ stack named pages.
As I have no clear() function to clear a stack, I wrote the following code:

stack<string> pages;
//here is some operation
//now clearing the stack

Now my question: is there a better efficient way to clear the stack?
Thanks in advance.

ee2 ee2

In general you can't clear copying containers in O(1) because you need to destroy the copies. It's conceivable that a templated copying container could have a partial specialization that cleared in O(1) time that was triggered by a trait indicating the type of contained objects had a trivial destructor.

If you want to avoid loop.