stack是CSTL里的栈容器适配器严格遵守LIFO先进后出最后放进去的元素最先拿出来。必须头文件#includestack #includeiostream using namespace std;或者#includebits/stdc.h//C万能头文件 using namespace std;一.基本定义stack数据类型容器名//stack的定义 stackints1; //定义一个储存数据类型为int的stack容器s1 stackdoubles2; //定义一个储存数据类型为double的stack容器s2 stackstrings3; //定义一个储存数据类型为string的stack容器s3 stack结构体类型s4; //定义一个储存数据类型为结构体类型的stack容器s4 stackint s5[N]; //定义一个储存数据类型为int的stack容器数组,N为大小 stackint s6[N]; //定义一个储存数据类型为int的stack容器数组,N为大小二.stack中常用的成员函数1.push(x):入栈压栈把元素放在栈顶栈长度1stackintch; ch.push(10); ch.push(20); ch.push(30); //栈结构底-10,20,30-顶2.top():取栈顶元素只看不取返回栈顶值不删除coutch.top()//输出结果为30注意空栈调用top()会崩溃使用前必须先判断3.pop()出栈(弹栈)只删不返回删除栈顶元素ch.pop();//删除30 coutch.top.();//现在输出为20注意空栈调用pop()会也直接崩溃4.empty():判断是否为空栈空栈返回true,非空栈返回falseif(ch.empty()) cout栈空endl; else cout栈非空endl;5.size():获取元素个数coutch.size();//当前有两个元素结果为26.swap():交换两个栈stackinta,b; a.push(1); b.push(2); a.swap(b);三.栈的遍历stack不能用for循环和访问下标ch[i],只能循环取top-pop#include bits/stdc.h using namespace std; int main() { stackint ch; ch.push(1); ch.push(2); ch.push(3); // 只能从栈顶一个个拿 while (!ch.empty()) { cout ch.top() ; ch.pop(); } // 输出3 2 1 return 0; }四.完整安全的写法永远先判断空再top/pop,防止崩溃#include bits/stdc.h using namespace std; int main() { stackint st; st.push(5); st.push(6); st.push(7); // 安全遍历 while (!st.empty()) { int val st.top(); cout val endl; st.pop(); } return 0; }五、栈的底层原理简单理解1.stack 不是独立容器是“包装器”2. 默认基于 deque 双端队列3.只开放尾部插入、尾部删除、尾部访问4.屏蔽了中间、头部的操作保证只能栈顶操作六.易错点总结非常重要1. ❌ 不能 st[0] 、 st.begin() 、 st.end() 遍历2. ❌ 空栈 top() / pop() 必崩溃3. ❌ pop() 没有返回值取值必须 top()4. ✅ 操作永远只能在栈顶5. ✅ 顺序一定后进先出
C++中stack的用法
发布时间:2026/5/22 20:19:40
stack是CSTL里的栈容器适配器严格遵守LIFO先进后出最后放进去的元素最先拿出来。必须头文件#includestack #includeiostream using namespace std;或者#includebits/stdc.h//C万能头文件 using namespace std;一.基本定义stack数据类型容器名//stack的定义 stackints1; //定义一个储存数据类型为int的stack容器s1 stackdoubles2; //定义一个储存数据类型为double的stack容器s2 stackstrings3; //定义一个储存数据类型为string的stack容器s3 stack结构体类型s4; //定义一个储存数据类型为结构体类型的stack容器s4 stackint s5[N]; //定义一个储存数据类型为int的stack容器数组,N为大小 stackint s6[N]; //定义一个储存数据类型为int的stack容器数组,N为大小二.stack中常用的成员函数1.push(x):入栈压栈把元素放在栈顶栈长度1stackintch; ch.push(10); ch.push(20); ch.push(30); //栈结构底-10,20,30-顶2.top():取栈顶元素只看不取返回栈顶值不删除coutch.top()//输出结果为30注意空栈调用top()会崩溃使用前必须先判断3.pop()出栈(弹栈)只删不返回删除栈顶元素ch.pop();//删除30 coutch.top.();//现在输出为20注意空栈调用pop()会也直接崩溃4.empty():判断是否为空栈空栈返回true,非空栈返回falseif(ch.empty()) cout栈空endl; else cout栈非空endl;5.size():获取元素个数coutch.size();//当前有两个元素结果为26.swap():交换两个栈stackinta,b; a.push(1); b.push(2); a.swap(b);三.栈的遍历stack不能用for循环和访问下标ch[i],只能循环取top-pop#include bits/stdc.h using namespace std; int main() { stackint ch; ch.push(1); ch.push(2); ch.push(3); // 只能从栈顶一个个拿 while (!ch.empty()) { cout ch.top() ; ch.pop(); } // 输出3 2 1 return 0; }四.完整安全的写法永远先判断空再top/pop,防止崩溃#include bits/stdc.h using namespace std; int main() { stackint st; st.push(5); st.push(6); st.push(7); // 安全遍历 while (!st.empty()) { int val st.top(); cout val endl; st.pop(); } return 0; }五、栈的底层原理简单理解1.stack 不是独立容器是“包装器”2. 默认基于 deque 双端队列3.只开放尾部插入、尾部删除、尾部访问4.屏蔽了中间、头部的操作保证只能栈顶操作六.易错点总结非常重要1. ❌ 不能 st[0] 、 st.begin() 、 st.end() 遍历2. ❌ 空栈 top() / pop() 必崩溃3. ❌ pop() 没有返回值取值必须 top()4. ✅ 操作永远只能在栈顶5. ✅ 顺序一定后进先出