📜  C++库-

📅  最后修改于: 2020-12-15 04:35:49             🧑  作者: Mango


介绍

堆栈是一种旨在在后进先出(LIFO)上下文中运行的数据结构。在堆栈中,元素只能从一端插入,也可以从一端移除。

堆栈类是容器适配器。容器是保存相同类型数据的对象。可以从不同的序列容器创建堆栈。如果未提供容器,则使用默认的双端队列容器。容器适配器不支持迭代器,因此我们不能将其用于数据操作。但是,它们分别支持push()pop()成员函数用于数据插入和删除。

定义

下面是头文件中std :: stack的定义

template  > class stack;

参量

  • T-所包含元素的类型。

    T可以用任何其他数据类型(包括用户定义的类型)代替。

  • 容器-基础容器对象的类型。

会员类型

成员函数可以将以下成员类型用作参数或返回类型。

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&

中的函数

以下是标头中的所有方法的列表。

建设者

Sr.No. Method & Description
1 stack::stack default constructor

Constructs an empty stack object, with zero elements.

2 stack::stack copy constructor

Constructs a stack with copy of each elements present in another stack.

3 stack::stack move constructor

Constructs a stack with the contents of other using move semantics.

析构函数

Sr.No. Method & Description
1 stack::~stack

Destroys stack by deallocating container memory.

会员职能

Sr.No. Method & Description
1 stack::emplace

Constructs and inserts new element at the top of stack.

2 stack::empty

Tests whether stack is empty or not.

3 stack::operator= copy version

Assigns new contents to the stack by replacing old ones.

4 stack::operator= move version

Assigns new contents to the stack by replacing old ones.

5 stack::pop

Removes top element from the stack.

6 stack::push copy version

Inserts new element at the top of the stack.

7 stack::push move version

Inserts new element at the top of the stack.

8 stack::size

Returns the total number of elements present in the stack.

9 stack::swap

Exchanges the contents of stack with contents of another stack.

10 stack::top

Returns a reference to the topmost element of the stack.

非成员重载函数

Sr.No. Method & Description
1 operator==

Tests 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 swap

Exchanges the contents of two stack.