arrays - 为什么将 r 值分配给数组下标内的变量
问题描述
我的推送功能有问题。数组下标(顶部)内的变量正在使用 r 值(项目)初始化。
当我使用 top=0 执行相同的操作时,它工作正常。
仅当 top=-1 时才会出现问题。
在第二次和第三次函数调用中,top 的值是按顺序递增的,而不是由 r-value(item) 初始化的
我每次都打印最高值以简化调试。
class stack { int top; public: int arr[500]; stack() { top=-1; } void push(int); }; void stack::push(int item) { arr[top++]=item; cout<<"\n"<<item<<"pushed in stack"; cout<<"\n top value is"<<top; } int main() { stack s; s.push(12); s.push(19); s.push(31); return 0; }
我预计输出为“12 压入堆栈且顶部值 = 0,但实际输出为 12 压入堆栈且顶部值为 12
解决方案
推荐阅读
- javascript - 在 Enzyme 中,如何从函数组件的 props 中获取函数?
- data-structures - 跳跃游戏 leetcode 问题中的预期输出不正确
- azure-powershell - 在 powershell 中拆分后如何选择每个第三个实例
- assembly - 试图从 LED 开关板中的开关和输出中读取
- c++ - 具有两种类型混淆 C++ 的模板
- php - 转换为准备好的语句
- angular - NGRX/RXJS 观察者在页面刷新时未发出
- python - 列表索引超出范围,使用 sort(key=lambda) 进行索引
- go - 如何将特定字符串转换为键和值的拆分“=”并在golang中将其转换为json
- jsf - “目标无法到达,标识符‘mainBean’解析为空”JSF