(3)stack.cpp
#include<iostream>
#include<stack>
using namespace std;
void test01()
{
//栈stack容器
//特点:符合先进后出数据结构
stack<int> s;
//入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout << "栈的大小: " << s.size() << endl;
//只要栈不为空,查看栈定,并且执行出栈操作
while (!s.empty())
{
//查看栈顶元素
cout << "栈顶元素为:" << s.top() << endl;
//出栈
s.pop();
}
cout << "栈的大小: " << s.size() << endl;
}
int main()
{
test01();
return 0;
}
该代码示例展示了如何在C++中使用stack容器进行数据操作,包括push(入栈)、pop(出栈)以及检查栈的size。它强调了栈作为先进后出(LIFO)数据结构的特点。
856

被折叠的 条评论
为什么被折叠?



