// Note while(!in.empty())&&(in.top()==popV[j]) and while((in.top()==popV[j])&&(!in.empty() ) The difference. The first judgment is empty and exit, but if you put j to the front, it will cause out of bounds. The logic is okay, but it is indeed out of bounds. You need to pay attention to the design in the future.