有些题目用栈还是很方便的,比如括号配对的问题,还有船舶进港出港的问题。
c++中有现成的STL可以使用
这里https://blog.csdn.net/liujiuxiaoshitou/article/details/53394888 有通过c语言实现栈的9种方式的代码,可以对栈的内核了解的更加清晰。
c++ 中可以使用的模板有
s.push(item); //将item压入栈顶
s.pop(); //删除栈顶的元素,但不会返回
s.top(); //返回栈顶的元素,但不会删除
s.size(); //返回栈中元素的个数
s.empty(); //检查栈是否为空,如果为空返回true,否则返回false
这些可以满足大多数基本需要
操作示例:
#include <iostream>
#include <cstdio>#include <algorithm>
#include <cstdio>
#include <cstring>
#include<cmath>
#include <vector>
#include <stack>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
stack<int>a;
int b,t;
cin>>b;
a.push(b);
cin>>b;
a.push(b);
//a.pop();
cout<<a.top();
return 0;
return 0;
}
后面很多需要与栈配合起来使用才能更简便,有蛮多升级操作不大会的,后面弄清楚了会陆续补充的