社区讨论

求调

B3614【模板】栈参与者 3已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@lp3gb0z2
此快照首次捕获于
2023/11/18 10:49
2 年前
此快照最后确认于
2023/11/18 13:38
2 年前
查看原帖
自己手打了一个用指针做的栈,但是出现了 RE 这种情况,代码如下,不知道咋回事:
CPP
#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

回复

4 条回复,欢迎继续交流。

正在加载回复...