stack implements the data structure that is followed first. The stack is allowed to be inserted (push) and delete (POP) at one end.
In the C ++, use a stack to introduce head files.
#include<stack>
When constructing a stack, it is necessary to specify the storage data type. It can be integer, character type, etc., or it can be a class or other types. For example.
stack<int> s; // The stack named S is used to store the integer data
stack<double> s2; // The stack named S2 is used to store dual -precision floating -point data
When there are two types of stacks with the same type, the assignment can be completed with the assignment number.
stack<int> s2;
s2 = s;
can also be constructed.
stack<int> s2(s);
stack can only access the top elements of the stack.
cout << s.top() << endl;
Note: This operation can only return to the top element of the stack, and cannot delete the top element of the stack. To delete the top element, then use it
pop()
Method.
s.empty();
If the stack is empty, returntrue
, otherwise returnfalse
。
s.size();
Return to the number of elements in the current stack.
s.push(2);
Press 2 into the stack.
s.pop();
Delete the top element of the stack.
Note that this operation can only delete the top element of the stack and cannot return the top element of the stack. If you want to return to the top element of the stack, use it
top()
Method.
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
stack<int> s2;
s2.push(100);
s2.push(200);
s2.push(300);
s.swap(s2);
exchange elements in two stacks.
Use it as follows
Theswap
method can also achieve the effect of exchange.
swap(s, s2)
More detailed content can be checkeddocumentation。