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

how to clear a stack in c
how to clear a stack in c++
stack c++
size of stack c++
empty function stack
empty in stack c++
c++ stack delete all
empty function in stack c++

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? Thanks in advance.

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);

how can i clear a stack in c++ efficiently?, What about subclassing std::stack and implementing a simple clear() method like this, accessing underlying container c ? public: void clear()� You can also retrieve the value using the Pop() method. The Pop() method removes and returns the value that was added last to the Stack. The Pop() method call on an empty stack will raise an InvalidOperationException. So always check for number of elements in stack must be greater than 0 before calling Pop() method. Pop() signature: object Pop();

Clear stack frame on return (C), You may probably do an intermediate function callout so, that the stack frame from the function foo is overwriten. Something like in the example below. By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used. Template parameters T Type of the elements. Aliased as member type stack::value_type. Container Type of the internal underlying container object where the elements are stored. Its value_type shall be T.

What about subclassing std::stack and implementing a simple clear() method like this, accessing underlying container c ?

public:
    void clear() { c.clear(); }

stack empty() and stack size() in C++ STL, empty() function is used to check if the stack container is empty or not. Check if the stack is empty, if not add the top element to a variable Why is the size of an empty class not zero in C++? � stack top() in C++ STL � Stack in� stack empty() and stack size() in C++ STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.

Stack Program in C, Stack Program in C - We shall see the stack implementation in C programming top = top - 1; return data; } else { printf("Could not retrieve data, Stack is empty. Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space; Stack Permutations (Check if an array is stack permutation of other) Infix to Postfix using different Precedence Values for In-Stack and Out-Stack; Find maximum in stack in O(1) without using additional stack; Sort a stack using a temporary

Stack Implementation in C, This articles covers stack implementation in C language. The stack can be implemented as follows in C: Utility function to check if the stack is empty or not. If the stack is full, then it is said to be an Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Peek or Top: Returns top element of stack. isEmpty: Returns true if stack is empty, else false.

Stack in C/C++, So, we will understand each and every important concept involved in a stack in C /C++ in detail so that you would develop a clear understanding� If you are viewing the array as a series of chars, then the only way to clear out the data is to touch every entry. memset is probably the most effective way to achieve this. On the other hand, if you are choosing to view this as a C/C++ null terminated string, setting the first byte to 0 will effectively clear the string.

Comments
  • Have you tried assigning an empty stack to your stack?
  • No, thanks for your suggestion (y)
  • It worked, thanks.
  • You asked about efficiency. Creating a new stack may not be more efficient, as it will lead to more heap allocations. The underlying container will by default be a deque. If you use a deque or vector you will throw away the allocated memory you have already claimed for the container, which could otherwise be reused.
  • @PaulRooney the same argument could be used the other way around. Destroying types with trivial destructors in a loop could be more wasteful than a (possibly elided) allocation. Moreover, keeping the allocated memory may be the opposite of what he wants. It's all up to making an informed decision.
  • Is there a way to do this (in C++14) without having to explicitly state the type?
  • All containers have clear -- except array. Is that the reason stack does not have clear? If so, wouldn't it be more useful to avoid or ignore that exception? Not having clear for stack seems weird, when you're using it.
  • @JohnH. Yes: pages = {};
  • When I use it, it empties all other stacks, I am not sure what's wrong.
  • It can be the case that all your stack are just references of one actual object. @Joseph.
  • This is non-standard and a bad idea for a few reasons. The underlying container name will vary depending on the implementation and it is not guaranteed that the implementation doesn't rely on some variables other than the underlying container, and clearing it could corrupt the state of the stack.