c++中的stack

xc889078 2013-06-12 09:42:19
#include <iostream>
#include <stack>
using namespace std;

int main()
{
stack<int> stack1;
stack1.push(1);
stack1.push(2);
int &data = stack1.top();
stack1.pop();
cout<<data<<endl;//输出2
data = stack1.top();
stack1.pop();
cout<<data<<endl;//输出-572662307
return 0;
}

本来压入的是1、2,出栈的顺序应该是2、1,但2正常出栈,1出栈时发生错误,但是如果把data变量改为int data,不将其设为引用变量,则正常出栈,即2、1顺序。
小弟初学c++,对stl代码没有研究过,还请各位帮忙解答一下,谢谢!!!
...全文
225 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-06-13
  • 打赏
  • 举报
回复
十八道胡同 2013-06-13
  • 打赏
  • 举报
回复
引用 13 楼 ForestDB 的回复:
可以读一下STL的源码 或者直接用debugger跟踪程序的执行
直接查看int& 指向的内存是否变化
ForestDB 2013-06-12
  • 打赏
  • 举报
回复
可以读一下STL的源码 或者直接用debugger跟踪程序的执行
xc889078 2013-06-12
  • 打赏
  • 举报
回复
引用 11 楼 ForestDB 的回复:
int &data = stack1.top(); // (1) data = stack1.top(); // (2) LZ觉得这两句的语义是什么? 个人的理解是: data引用(1)处的stack1.top()(一旦引用,不能引用别的东西),运行到(2)时,再用(2)这时的stack1.top()来修改(1)那个时候的stack1.top() 是不是有点绕? 抛开stack,单看引用:

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int & b = a;
    b = 2;
    cout << a << '\n' << b << '\n';

    return 0;
}
恩 这点我写的不够严谨。 但现在我想知道pop()有没有释放掉栈顶变量的内存,还是只将栈顶指针和栈的大小减1而已,因为没研究过stl,所以这块不是太明白
ForestDB 2013-06-12
  • 打赏
  • 举报
回复
int &data = stack1.top(); // (1) data = stack1.top(); // (2) LZ觉得这两句的语义是什么? 个人的理解是: data引用(1)处的stack1.top()(一旦引用,不能引用别的东西),运行到(2)时,再用(2)这时的stack1.top()来修改(1)那个时候的stack1.top() 是不是有点绕? 抛开stack,单看引用:

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int & b = a;
    b = 2;
    cout << a << '\n' << b << '\n';

    return 0;
}
xc889078 2013-06-12
  • 打赏
  • 举报
回复
引用 7 楼 LCL_data 的回复:
[quote=引用 5 楼 xc889078 的回复:] [quote=引用 2 楼 LCL_data 的回复:] 区别在于int & 和int 前面一个指向int的一个引用,后者是一个int变量 data = stack1.top(); 此时int&指向了1这个变量的地址,立即输出data的值 是1 stack1.pop(); 1这个变量的地址被释放,之后在输出data的值 ,什么值都有可能
我觉得pop()并没有释放变量内存,而是将栈的栈顶指针减1[/quote] void pop ( ); Remove element Removes the element on top of the stack, effectively reducing its size by one. The value of this element can be retrieved before being popped by calling member stack::top. This calls the removed element's destructor. This member function effectively calls the member function pop_back of the underlying container object (such as deque::pop_back). pop操作调用了int的析构函数[/quote] 我觉得这和编译器有关,我用百度云IDE也能得到正确结果。但照您所说,如果pop()把变量释放,那2,1是怎么被正确打印出来的呢?
buyong 2013-06-12
  • 打赏
  • 举报
回复
我觉得是编译器的问题。但是这种写法肯定不好。
十八道胡同 2013-06-12
  • 打赏
  • 举报
回复
引用 4 楼 xc889078 的回复:
[quote=引用 2 楼 LCL_data 的回复:] 区别在于int & 和int 前面一个指向int的一个引用,后者是一个int变量 data = stack1.top(); 此时int&指向了1这个变量的地址,立即输出data的值 是1 stack1.pop(); 1这个变量的地址被释放,之后在输出data的值 ,什么值都有可能
但是2的值是正常得到的啊[/quote] http://www.cplusplus.com/reference/stack/stack/pop/
十八道胡同 2013-06-12
  • 打赏
  • 举报
回复
引用 5 楼 xc889078 的回复:
[quote=引用 2 楼 LCL_data 的回复:] 区别在于int & 和int 前面一个指向int的一个引用,后者是一个int变量 data = stack1.top(); 此时int&指向了1这个变量的地址,立即输出data的值 是1 stack1.pop(); 1这个变量的地址被释放,之后在输出data的值 ,什么值都有可能
我觉得pop()并没有释放变量内存,而是将栈的栈顶指针减1[/quote] void pop ( ); Remove element Removes the element on top of the stack, effectively reducing its size by one. The value of this element can be retrieved before being popped by calling member stack::top. This calls the removed element's destructor. This member function effectively calls the member function pop_back of the underlying container object (such as deque::pop_back). pop操作调用了int的析构函数
十八道胡同 2013-06-12
  • 打赏
  • 举报
回复
引用 3 楼 xc889078 的回复:
[quote=引用 1 楼 lhfslhfs 的回复:] 我的qt+ MINGW 运行完全正常啊。 你用的什么编译器?
VC++6.0[/quote] 我的VS2008 运行也正常
xc889078 2013-06-12
  • 打赏
  • 举报
回复
引用 2 楼 LCL_data 的回复:
区别在于int & 和int 前面一个指向int的一个引用,后者是一个int变量 data = stack1.top(); 此时int&指向了1这个变量的地址,立即输出data的值 是1 stack1.pop(); 1这个变量的地址被释放,之后在输出data的值 ,什么值都有可能
我觉得pop()并没有释放变量内存,而是将栈的栈顶指针减1
xc889078 2013-06-12
  • 打赏
  • 举报
回复
引用 2 楼 LCL_data 的回复:
区别在于int & 和int 前面一个指向int的一个引用,后者是一个int变量 data = stack1.top(); 此时int&指向了1这个变量的地址,立即输出data的值 是1 stack1.pop(); 1这个变量的地址被释放,之后在输出data的值 ,什么值都有可能
但是2的值是正常得到的啊
xc889078 2013-06-12
  • 打赏
  • 举报
回复
引用 1 楼 lhfslhfs 的回复:
我的qt+ MINGW 运行完全正常啊。 你用的什么编译器?
VC++6.0
十八道胡同 2013-06-12
  • 打赏
  • 举报
回复
区别在于int & 和int 前面一个指向int的一个引用,后者是一个int变量 data = stack1.top(); 此时int&指向了1这个变量的地址,立即输出data的值 是1 stack1.pop(); 1这个变量的地址被释放,之后在输出data的值 ,什么值都有可能
青松2 2013-06-12
  • 打赏
  • 举报
回复
我的qt+ MINGW 运行完全正常啊。 你用的什么编译器?

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧