title: What is the best way to clear out a Deque of pointers?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/716762/c-stl-which-method-of-iteration-over-a-stl-container-is-better">C++ STL: Which method of iteration over a STL container is better?</a>  </p>
  </blockquote>
  
  
  
  <p>In my current project, I have an STL Deque of pointers. I also have a method that is meant to delete all of the objects that those pointers are pointing to. I've come up with two different ways to achieve this, but I can't decide which is the preferred method.</p>
  
  <p><strong>Method 1.</strong></p>
  
  <pre><code>for (deque&lt;MyType*&gt;::const_iterator it = myDeque.begin(); it != myDeque.end(); ++it)
  {
      delete *it;
  }
  
  myDeque.clear();
  </code></pre>
  
  <p><strong>Method 2.</strong></p>
  
  <pre><code>for (int i = 0; i &lt; myDeque.size(); ++i)
  {
      delete myDeque[i];
  }
  
  myDeque.clear();
  </code></pre>
  
  <p>Both of these methods should work, but which one would be preferred? Method 1 makes use of the STL, but Method 2 is much simpler. Other than code cleanliness, is there any reason why one method should be used over the other? Is there any advantage to using an iterator in this scenario despite the little bit of overhead that goes into creating it?</p>
  
  <p>Note: This question applies to other STL sequence containers, not just Deques.</p>