c++

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

小樊
81
2024-09-25 08:33:11
栏目: 编程语言

在 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.”。

0
看了该问题的人还看了