【STL】stack容器

mac2025-02-26  19

stack容器

1-特性总结 (1)先进后出 (2)栈不提供迭代器,因此不能遍历,不支持随机存取,只能通过top从栈顶获取和删除元素 (3)只能通过栈顶一个一个给里面加元素,通过栈顶一个一个删除元素

实例

#include<iostream> #include<stack> using namespace std; void test01() { //1-初始化 stack<int>s1; stack<int>s2(s1); //2-stack操作 s1.push(10); s1.push(20); s1.push(30); s1.push(100); cout << "栈顶元素:" << s1.top() << endl; s1.pop();//删除栈顶元素 //打印栈容器的数据 while (!s1.empty()) { cout << s1.top() << " "; s1.pop(); } cout << s1.size() << endl; } int main() { test01(); return 0; }
最新回复(0)