title: What is the right way to free a std::vector of pointers in C++?
body: <p>I searched StackOverflow but couldn't find the answer to this question.</p>
  
  <p>Suppose I have a <code>std::vector&lt;Day *&gt; vector_day</code> - that is - a vector of pointers to <code>Day</code> object. Now I <code>push_back</code> to <code>vector_day</code> many elements:</p>
  
  <pre><code>vector_day.push_back(new Day(12));
  vector_day.push_back(new Day(99));
  vector_day.push_back(new Day(71));
  ...
  </code></pre>
  
  <p>Now at some point I no longer need <code>vector_day</code>. What is the right way to free the memory?</p>
  
  <p>It this the correct way:</p>
  
  <pre><code>for (std::vector&lt;Day *&gt;::iterator i = vector_day.begin(); i != vector_day.end(); ++i) {
      delete *i;
  }
  </code></pre>
  
  <p>Doesn't this invalidate the vector on each deletion? I am very confused.</p>