阅读量:109
在C++中,你可以使用top()函数来获取栈顶元素。以下是一个示例:
#include
#include
int main() {
std::stack<int> myStack;
myStack.push(1);
myStack.push(2);
myStack.push(3);
// 获取栈顶元素
int topElement = myStack.top();
std::cout << "栈顶元素: " << topElement class="hljs-keyword">return 0;
}
输出:
栈顶元素: 3