温馨提示×

c++ stack类怎样检查是否为空

c++
小樊
99
2024-09-25 08:33:11
栏目: 编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在 C++ 标准库中,stack 类没有直接提供一个成员函数来检查栈是否为空

#include <iostream>
#include <stack>

int main() {
    std::stack<int> myStack;

    if (myStack.empty()) {
        std::cout << "The stack is empty." << std::endl;
    } else {
        std::cout << "The stack is not empty." << std::endl;
    }

    return 0;
}

在这个示例中,我们创建了一个空的 std::stack<int> 对象,并使用 empty() 函数检查它是否为空。如果栈为空,程序将输出 “The stack is empty.”,否则将输出 “The stack is not empty.”。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:怎样判断c++ stack类是否为空

0