How can I clear a stack in c++ efficiently?
c++
Solution
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.
pages=stack<std::string>();
or
stack<std::string>().swap(pages);
Problem
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 while(!pages.empty()) pages.pop(); ``` Now my question: is there a better efficient way to clear the stack?