c++ - How do i fix this code so that it wont pop_back or back() if the vector/stack is empty? -
there wrong stack.h , i'm unsure missing. receiving segmentation fault error. know has "void pop()" , "t top()" functions. i'm pretty sure caused empty stack. editing these 2 functions, how can ensure program run? .cpp file needs have s2.pop outside of (!s2.empty) check.
the answer should remove s2.pop() @ end. doesn't make sense there.
while (!s2.empty()) { cout << s2.top(); s2.pop(); } // s2 empty now, pop() wouldn't make sense. cout << endl; s2.pop(); you change pop() function to
void pop() { if (!empty()) container.pop_back(); } then pop() work on empty stack, top() still crash. can't fix top() easy either, you'd end different behaviours confusing , shouldn't have. or unintuitive implementations bad.
Comments
Post a Comment