Skip to content

模拟栈

用数组来模拟栈,不用 STL,提高 C++ 代码的运行速度。

C++
const int N = 1e4;
int tt = -1;        // 栈顶下标
int stk[N];         // 模拟栈

stk[tt];            // 返回栈顶元素

tt --;              // 弹出栈顶元素

if (tt >= 0) {
    ...             // 栈非空
}

stk[++ tt] = x;     // 将 x 入栈

Released under the MIT License.