title: How to make a stack grow dynamically?
body: <p>Here is a little program with my Stack class:</p>
  
  <pre><code> #include &lt;iostream&gt;
  
  using namespace std;
  
  #include &lt;iomanip&gt;
  
  template &lt;typename T&gt;
  class Stack
  {
  private:
      T *stackPtr; 
      int size; 
      T top; 
  public:
      Stack(int = 10);
      ~Stack(); 
      bool push(const T  );
      bool pop(); 
      void printStack();
  };
  
  int main()
  {
      Stack &lt;int&gt; myStack(5);
  
  
      cout &lt;&lt; "Push 5 elements to stack: ";
      int ct = 0;
      while (ct++ != 5)
      {
          int temp;
          cin &gt;&gt; temp;
          myStack.push(temp);
      }
  
      myStack.printStack(); 
  
      cout &lt;&lt; "\nErasing two elements:\n";
  
      myStack.pop(); 
      myStack.pop(); 
      myStack.printStack(); 
  
      return 0;
  }
  
  
  template &lt;typename T&gt;
  Stack&lt;T&gt;::Stack(int s)
  {
      size = s &gt; 0 ? s: 10;   
      stackPtr = new T[size]; 
      top = -1; 
  }
  
  
  template &lt;typename T&gt;
  Stack&lt;T&gt;::~Stack()
  {
      delete [] stackPtr; 
  }
  
  
  template &lt;typename T&gt;
  bool Stack&lt;T&gt;::push(const T value)
  {
      if (top == size - 1)
          return false; 
  
      top++;
      stackPtr[top] = value; 
  
      return true; 
  }
  
  
  template &lt;typename T&gt;
  bool Stack&lt;T&gt;::pop()
  {
      if (top == - 1)
          return false; 
  
      stackPtr[top] = 0; 
      top--;
  
      return true; 
  }
  
  
  template &lt;typename T&gt;
  void Stack&lt;T&gt;::printStack()
  {
      for (int ix = size -1; ix &gt;= 0; ix--)
          cout &lt;&lt; "|" &lt;&lt; setw(4) &lt;&lt; stackPtr[ix] &lt;&lt; endl;
  }
  </code></pre>
  
  <p>So, in order to use such a stack, I should declare it's size in the constructor before usage, just like <code>Stack&lt;int&gt; newstack(10)</code>, if I need 10 elements. So, what to do, if I don't know the final size of a stack? How to make it grow dynamically, just by pushing elements to it? I was searching for a solution, but still all my ideas come to counting elements and then declaring a stack to fit elements number. </p>