C++ Stack 库 - empty() 函数
描述
C++ 函数 std::stack::empty() 测试堆栈是否为空。 大小为零的堆栈被视为空堆栈。
声明
以下是 std::stack::empty() 函数形式 std::stack 头的声明。
C++98
bool empty() const;
参数
None
返回值
如果堆栈为空,则返回 true,否则返回 false。
异常
为标准容器提供 no-throw 保证。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::stack::empty() 函数的用法。
#include <iostream>
#include <stack>
using namespace std;
int main(void) {
stack<int> s;
if (s.empty())
cout << "Stack is empty." << endl;
s.emplace(1);
if (!s.empty())
cout << "Stack is not empty." << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Stack is empty. Stack is not empty.

