相关疑难解决方法(0)

为什么pop()无效?

对于堆栈pop()是无效的,这意味着每当我想要获得顶部和弹出时我需要两行代码:

auto top = s.top();
s.pop();
Run Code Online (Sandbox Code Playgroud)

如果我能这样做会很方便:

auto top = s.pop();
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

c++ stack

8
推荐指数
0
解决办法
261
查看次数

使用C++中定义的堆栈stl

#include <stack>
using namespace std;

int main() {
    stack<int> s;
    int i;
    for (i = 0; i <= 10; i++) {
        s.push(i);
    }
    for (i = 0; i <= 10; i++) {
        printf("%d", s.pop());
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码有什么问题?

错误:

函数int main():在预期整数时使用的聚合值

c++ stack stl

5
推荐指数
1
解决办法
478
查看次数

标签 统计

c++ ×2

stack ×2

stl ×1