std::stack
is a container library class (of which there are several classes) implemented as template
.
In English stack means: stack, and that’s exactly what it does, it stores the data in a stacked way.
That is, a stack has the data structure LIFO(Last-In, First-Out).
A stack is different from a list, the stack has the idea as a “stack of dishes”, if you STACK the dishes and try to get the first one (which is at the bottom of the pile, down there!) , all above fall! In no list, you can take from any position depending on the subset of the type of list you create in structuring the data.
So, on the stack, you can only remove the last element you added.
Some member functions of std::stack
are similar to or implemented from other classes in the Containers library, such as:
push()
pop()
empty()
And among others.
Let’s see how to use them!
To use std::stack
you first need to add the <stack>
library:
Declare the stack:
Add items/elements to stack:
Know who is on top of the stack, last to be added:
To delete the last element (the top one)
Check if it is empty, remove and print:
The complete code:
For more tips on std::stack
go to here .