定义于头文件 <stack>
template< class T, |
std::stack
类是容器适配器,它给予程序员栈的功能——特别是 FILO (先进后出)数据结构。
该类模板表现为底层容器的包装器——只提供特定函数集合。栈从被称作栈顶的容器尾部推弹元素。
赋值给容器适配器
std::stack<T,Container>::operator=
stack& operator=( const stack& other ); | (1) | |
stack& operator=( stack&& other ); | (2) | (C++11 起) |
以 other
的内容替换容器适配器的内容。
1) 复制赋值运算符。以 other
的内容副本替换内容。等效地调用 c = other.c; 。(隐式声明)
2) 移动赋值运算符。用移动语义以 other
的内容替换内容。等效地调用 c = std::move(other.c); 。(隐式声明)
参数
other | - | 用作源的另一容器适配器 |
返回值
*this
复杂度
等价于底层容器 operator= 的复杂度。
容量
检查底层的容器是否为空
std::stack<T,Container>::empty
bool empty() const; | (C++20 前) | |
[[nodiscard]] bool empty() const; | (C++20 起) |
检查底层容器是否为空,即是否 c.empty() 。
参数
(无)
返回值
若底层容器为空则为 true ,否则为 false 。
复杂度
常数
返回容纳的元素数
std::stack<T,Container>::size
size_type size() const; |
返回底层容器中的元素数,即 c.size() 。
参数
(无)
返回值
容器中的元素数。
复杂度
常数。
调用示例