title: Wish for Better c++ Stack Implementation using Vectors
body: <p>I am using Microsoft VS 2013 on Windows 7 64-bit </p>
  
  <p>I found the following simple Stack implementation on TutorialsPoint </p>
  
  <p><a href="http://www.tutorialspoint.com/cplusplus/cpp_templates.htm" rel="nofollow">http://www.tutorialspoint.com/cplusplus/cpp_templates.htm</a> </p>
  
  <p>Though this was just a simple example, I wanted whatever was "popped" be returned to the caller.  As it is now, you have to access top() and then pop() the top for the functionality that I want.  </p>
  
  <p>I tried to change "void pop()" to "T pop()" and then modify the return type of pop() in the implementation, but the compiler balked about not being able to convert string types.  </p>
  
  <p>First, shouldn't a better implementation return the value "popped" </p>
  
  <p>and, if so, </p>
  
  <p>How do I modify the code so that I return was it "popped" ? </p>
  
  <p>If not clear, I would like to use: </p>
  
  <pre><code>        cout &lt;&lt; stringStack.pop() &lt;&lt; std::endl; 
  </code></pre>
  
  <p>instead of the two lines needed below. </p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;vector&gt; 
  #include &lt;cstdlib&gt; 
  #include &lt;string&gt; 
  #include &lt;stdexcept&gt; 
  
  using namespace std; 
  
  template &lt;class T&gt; 
  class Stack 
  { 
  private: 
      vector&lt;T&gt; elems;                // elements 
  
  public: 
      void push( T const&amp; );      // push element 
      void pop();         // pop element 
      T top() const;          // return top element 
      bool empty() const      // return true if empty. 
      { 
          return elems.empty(); 
      } 
  }; 
  
  template &lt;class T&gt; 
  void Stack&lt;T&gt;::push (T const&amp; elem) 
  { 
      // append copy of passed element 
      elems.push_back(elem); 
  } 
  
  template &lt;class T&gt; 
  void Stack&lt;T&gt;::pop () 
  { 
      if (elems.empty()) 
      { 
          throw out_of_range( "Stack&lt;&gt;::pop(): empty stack" ); 
  } 
  
  // remove last element 
  elems.pop_back(); 
  
  } 
  
  template &lt;class T&gt; 
  T Stack&lt;T&gt;::top () const 
  { 
      if (elems.empty()) 
      { 
          throw out_of_range( "Stack&lt;&gt;::top(): empty stack" ); 
      } 
  
      // return copy of last element 
      return elems.back(); 
  
  } 
  
  int main() 
  { 
      try 
              { 
          Stack&lt;int&gt;      intStack;   // stack of ints 
          Stack&lt;string&gt;   stringStack;    // stack of strings 
  
          // manipulate int stack 
          intStack.push( 7 ); 
          cout &lt;&lt; intStack.top() &lt;&lt;endl; 
  
          // manipulate string stack 
          stringStack.push( "hello" ); 
          stringStack.push( "world" ); 
          cout &lt;&lt; stringStack.top() &lt;&lt; std::endl; 
          stringStack.pop(); 
          cout &lt;&lt; stringStack.top() &lt;&lt; std::endl; 
          stringStack.pop(); 
          cout &lt;&lt; stringStack.top() &lt;&lt; std::endl; 
  
          return 0;
  
      } 
      catch ( exception const&amp; ex ) 
      { 
          cerr &lt;&lt; "Exception: " &lt;&lt; ex.what() &lt;&lt;endl; 
          return -1; 
  
      } 
  
  } 
  </code></pre>