Possible Duplicate:
C++ STL: Which method of iteration over a STL container is better?
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.
Method 1.
for (deque<MyType*>::const_iterator it = myDeque.begin(); it != myDeque.end(); ++it)
{
delete *it;
}
myDeque.clear();
Method 2.
for (int i = 0; i < myDeque.size(); ++i)
{
delete myDeque[i];
}
myDeque.clear();
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?
Note: This question applies to other STL sequence containers, not just Deques.