Sequence container in STL-stack (stack)

Sequence container in STL-stack (stack)

  stack, that is, "stack". The stack is a sequence of last-in-first-out (LIFO) elements. Access and deletion can only be performed on the element at the top of the stack (that is, the last element added to the stack), and elements can only be added to the top of the stack. The elements in the stack cannot be accessed. If you must access the elements in the stack, you can only delete all the elements above it from the stack and make it the top element of the stack.
  Stack is an important member of C++ STL. When using it, you need to include header files:

#include <stack>;

  Stack is a type of container adapter, and the data in the container adapter is organized in LIFO. Only the elements at the top of the stack can be accessed; the elements below can only be accessed after the elements at the top of the stack are removed.

1. Initialization of stack

There are the following ways, examples are as follows:

stack<int> a;
stack<int> b(a); //拷贝栈a给栈b

Two, important operations of stack objects

Listed as follows:

a.push(5); //往栈头添加元素5
a.pop(); //从栈头移除第一个元素
a.top(); //提取最后一个压入栈元素
a.empty(); //判断a是否为空,空则返回ture,不空则返回false
a.size(); //返回a中元素的个数
a=b; //栈b赋值给栈a

Guess you like

Origin blog.csdn.net/hyl1181/article/details/108564820