使用C ++ stl中定义的堆栈 [英] Using stack defined in C++ stl

查看:196
本文介绍了使用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());
    }
}

上述代码有什么问题?

错误:


int main():需要整数时使用的汇总值

In function int main(): aggregate value used where an integer was expected


推荐答案

stack :: pop 是一个 void 函数,它只是丢弃堆栈上的顶层元素,以获取要使用的值 stack :: top

stack::pop is a void function which just discards the top element on the stack, in order to get the value you want to use stack::top.

这是因为异常安全原因(如果返回的对象在其拷贝构造函数中抛出异常会发生什么?)。

The reason this is so is for exception safety reasons (what happens if the object returned throws an exception in its copy constructor?).

这篇关于使用C ++ stl中定义的堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆