#include <string> #include <stack> #include <iostream> std::stack<std::string> mystack; if (mystack.size() == 0) { std::cout << "Stack is empty" << std::endl; } else { std:string str = mystack.top(); mystack.pop(); std::cout << "Popped " << str << std::endl; }
Programming Tips - How can I avoid pop()ing an empty std::stack ?
Date: 2016aug11
Language: C++
Q. How can I avoid pop()ing an empty std::stack ?
A. Check size() first, like this: