自己手打了一个用指针做的栈,但是出现了 RE 这种情况,代码如下,不知道咋回事:
#include<bits/stdc++.h>
using namespace std;
// #define fileio
#define IOS
void ___()
{
#ifdef fileio
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
#ifdef IOS
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
#endif
}
typedef unsigned long long ull;
namespace jqQt0220
{
template<typename T> class stack
{
private:
T *val;
T *beg=val;
public:
inline void push(T _x)
{
*++val=_x;//问题可能出在这
}
inline void pop()
{
if(!empty())
val--;
}
inline T top()
{
return *val;
}
inline int size()
{
return val-beg;
}
inline bool empty()
{
return val>beg;
}
inline void clear()
{
val=beg;
}
};
}
jqQt0220::stack<ull> s;
int n;
int main()
{
___();
int _;
cin>>_;
while(_--)
{
s.clear();
cin>>n;
while(n--)
{
string op;
cin>>op;
if(op=="push")
{
ull x;
cin>>x;
s.push(x);
}
if(op=="pop")
{
if(s.empty())
cout<<"Empty"<<endl;
else
s.pop();
}
if(op=="query")
{
if(s.empty())
cout<<"Anguei!"<<endl;
else
cout<<s.top()<<endl;
}
if(op=="size")
{
cout<<s.size()<<endl;
}
}
}
return 0;
}
(验证码 cyka