title: Releasing memory reserved by std::vector and its elements in descrutor of class that holds it
body: <p>I would like to know if I should manually release the memory reserved by <code>std::vector</code> and its elements in the destructor of the class that contains that vector. And if so - how exactly?</p>
  
  <p><strong>Situation #1 - std::vector of primitive types:</strong></p>
  
  <pre><code>class A{
      std::vector&lt;int&gt; elements;
      A(){...}
  };
  </code></pre>
  
  <p><strong>Situation #2 - std::vector of complex types:</strong></p>
  
  <pre><code>class B{
      int b;
      C * pointer;
      ...
  };
  
  class A{
      std::vector&lt;B&gt; elements;
      A(){...}
  };
  </code></pre>
  
  <p><strong>Situation #3 - std::vector of pointers to complex types:</strong></p>
  
  <pre><code>class B{
      int b;
      C * pointer;
      ...
  };
  
  class A{
      std::vector&lt;B*&gt; elements;
      A(){...}
  };
  </code></pre>
  
  <p>In which cases the destructor of A should be empty? In which it should look like:</p>
  
  <pre><code>A::~A(){
      for(auto &amp;e : elements){
          delete e;
      }
      elements.clear();
  }
  </code></pre>
  
  <p>I think about situation, when <code>elements</code> belongs only to <code>A</code> and are not shared by any other structure (<strong>when <code>A</code> is destroyed, the <code>elements</code> should also be destroyed</strong>).</p>
  
  <p><strong>It seems obvious that for <code>std::vector</code> of pointers, I have to use <code>delete e</code> on each element</strong>. </p>
  
  <p>But:</p>
  
  <ol>
  <li>Do I really have to call <code>elements.clear();</code> (the object will be destroyed after that line - isn't it waste of instructions)?</li>
  <li>Maybe I should also use <code>erase(...)</code> method of <code>std::vector</code> in loop? <strong>If so</strong>, wouldn't <code>clear()</code> make all the work for me instead (when the <code>std::vector</code> does not contains pointers)?</li>
  </ol>
  
  <p>Did I miss something? <strong>What's the best approach?</strong></p>
  
  <p>p.s. Situation #3 is about pointers and <strong>not a shared pointers</strong>.</p>