Cpp Standard Library 简明教程
C++ Library - <stack>
Introduction
堆栈是一种数据结构,设计用于在 LIFO(后进先出)上下文中操作。在堆栈中,元素仅从一端插入和移除。
Stack is a data structure designed to operate in LIFO (Last in First out) context. In stack elements are inserted as well as get removed from only one end.
Stack 类是一个容器适配器。容器是容纳相同类型数据的对象。Stack 可以在不同的顺序容器中创建。如果未提供容器,它将使用默认 deque 容器。容器适配器不支持迭代器,因此我们不能将其用于数据操作。但是,它们支持成员函数 push() 和 pop(),分别用于数据插入和移除。
Stack class is container adapter. Container is an objects that hold data of same type. Stack can be created from different sequence containers. If container is not provided it uses default deque container. Container adapters do not support iterators therefore we cannot use them for data manipulation. However they support push() and pop() member functions for data insertion and removal respectively.
Definition
下面是 <stack> 头文件中 std::stack 的定义
Below is definition of std::stack from <stack> header file
template <class T, class Container = deque<T> > class stack;
Parameters
-
T − Type of the element contained. T may be substituted by any other data type including user-defined type.
-
Container − Type of the underlying container object.
Member types
以下成员类型可作为参数或返回类型,由成员函数使用。
Following member types can be used as parameters or return type by member functions.
Sr.No. |
Member types |
Definition |
1 |
value_type |
T (First parameter of the template) |
2 |
container_type |
Second parameter of the template |
3 |
size_type |
size_t |
4 |
reference |
value_type& |
5 |
const_reference |
const value_type& |
Functions from <stack>
下面是 <stack> 头文件的所有方法列表。
Below is list of all methods from <stack> header.
Constructors
Sr.No. |
Method & Description |
1 |
stack::stack default constructorConstructs an empty stack object, with zero elements. |
2 |
stack::stack copy constructorConstructs a stack with copy of each elements present in another stack. |
3 |
stack::stack move constructorConstructs a stack with the contents of other using move semantics. |
Destructor
Sr.No. |
Method & Description |
1 |
stack::~stackDestroys stack by deallocating container memory. |
Member functions
Sr.No. |
Method & Description |
1 |
stack::emplaceConstructs and inserts new element at the top of stack. |
2 |
stack::emptyTests whether stack is empty or not. |
3 |
stack::operator= copy versionAssigns new contents to the stack by replacing old ones. |
4 |
stack::operator= move versionAssigns new contents to the stack by replacing old ones. |
5 |
stack::popRemoves top element from the stack. |
6 |
stack::push copy versionInserts new element at the top of the stack. |
7 |
stack::push move versionInserts new element at the top of the stack. |
8 |
stack::sizeReturns the total number of elements present in the stack. |
9 |
stack::swapExchanges the contents of stack with contents of another stack. |
10 |
stack::topReturns a reference to the topmost element of the stack. |
Non-member overloaded functions
Sr.No. |
Method & Description |
1 |
[role="bare"]../cpp_standard_library/cpp_stack_operator_equal_to.htmlTests whether two stacks are equal or not. |
2 |
operator!=Tests whether two stacks are equal or not. |
3 |
operator<Tests whether first stack is less than other or not. |
4 |
operator⇐Tests whether first stack is less than or equal to other or not. |
5 |
operator>Tests whether first stack is greater than other or not. |
6 |
operator>=Tests whether first stack is greater than or equal to other or not. |
7 |
swapExchanges the contents of two stack. |