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

Popular posts from this blog

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

C# random value from dictionary and tuple -

algorithm - Testing tetrahedron-triangle intersection -