c++ while loop condition isn't playing nice with stack.empty() [closed]

强颜欢笑 提交于 2019-12-12 17:21:33

问题


So I'm flustered by this bit of code - can't understand why it's misbehaving. The only thing I can think of is that stack.empty() isn't working properly with the while loop, but that seems ridiculous. You'll see in the output following the code that the program never makes it out of the while loop before the run fails.

declaration of opStack and newOrder:

queue<string> newOrder;
stack< vector<char> > opStack;

other stuff happens, then this code executes:

while(opStack.empty()==false){
    if(opStack.top()[1] != 'L'){
        cout<<"is stack empty?:"<<opStack.empty()<<endl;
        symbol = opStack.top()[0];
        newOrder.push(symbol);
        opStack.pop();
        cout<<"popped stack;"<<endl;
        cout<<"is stack empty?:"<<opStack.empty()<<endl;
    }
    else{
        break;
    }
}
cout<<"made it out of while loop";

output:

is stack empty?:0
popped stack;
is stack empty?:1
RUN FAILED (exit value 1, total time: 1s)

来源:https://stackoverflow.com/questions/13131803/c-while-loop-condition-isnt-playing-nice-with-stack-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!