c++

c++ stack怎么获取栈顶元素

小亿
209
2024-01-13 09:30:14
栏目: 编程语言
C++开发者专用服务器,限时0元免费领! 查看>>

在C++中,你可以使用top()函数来获取栈顶元素。以下是一个示例:

#include <iostream>
#include <stack>

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

    myStack.push(1);
    myStack.push(2);
    myStack.push(3);

    // 获取栈顶元素
    int topElement = myStack.top();
    std::cout << "栈顶元素: " << topElement << std::endl;

    return 0;
}

输出:

栈顶元素: 3

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

相关推荐:c++ stack类如何获取栈顶元素

0
看了该问题的人还看了