0

I feel like a stack in which pop/top are combined should already exist - am I missing something? Most languages have this. Does C++?

#include <stack>
#include <iostream>
template <typename T>
class gStack : public std::stack<T>
{
public:
    T giveNext()
    {
        T n = std::stack<T>::top();
        std::stack<T>::pop();
        return n;
    }
};



int main()
{
    gStack<int> s;
    s.push(3);
    std::cout << s.giveNext();
    return 0;
}
Carbon
  • 3,828
  • 3
  • 24
  • 51
  • Oh, cause you don't want to get to a spot where pop has been done and thus the stack is mutated but the fn hasn't returned? – Carbon May 26 '17 at 17:22
  • 1
    Your implementation copies the stack elements, which is usually avoidable. – Kerrek SB May 26 '17 at 17:32

0 Answers0