C++stack (Stack) is an adaptation of a container that implements an advanced post-out data structure (FILO)
You need to include the #include<stack> header file when using this container;
The sample code that defines the stack object is as follows:
stack<int>s1;
stack<string>s2;
The basic operations of stack are:
1. Into the stack: such as S.push (x);
2. Out of stack: such as S.pop (). Note: The stack operation simply removes the element at the top of the stack and does not return the element.
3. Access the top of the stack: such as s.top ();
4. Determine the empty stack: such as S.empty (). Returns True when the stack is empty.
5. Access the number of elements in the stack, such as s.size ();
Here's a simple example:
[CPP]View Plaincopyprint?
- #include <iostream>
- #include <stack>
- Using namespace std;
- int main (void)
- {
- stack<double>s; Define a stack
- For (int i=0;i<10;i++)
- S.push (i);
- While (!s.empty ())
- {
- printf ("%lf\n", S.top ());
- S.pop ();
- }
- cout<<"The number of elements in the stack is:" <<s.size () <<endl;
- return 0;
- }
"C + +" STL: Stack