   C++ vector problem
  Hi, so I'm reading "Beginning Game Programming" by Michael Morrison, and so far it's pretty good.  However there's a part of his code that is giving me problems and I tried to fix it but got stuck.The part of the code giving me problems is this:// Delete and remove the sprites in the sprite vector  vector<Sprite*>::iterator siSprite;  for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)  {    delete (*siSprite);    m_vSprites.erase(siSprite);    siSprite--;  }The code itself compiles, but when I try to close the program I get an error:---------------------------Microsoft Visual C++ Debug Library---------------------------Debug Assertion Failed!Program: ..._Threepwood\My Documents\Chap10\Planets 2\Debug\Planets 2.exeFile: c:\program files\microsoft visual studio 9.0\vc\include\vectorLine: 138Expression: ("this->_Has_container()", 0)For information on how your program can cause an assertionfailure, see the Visual C++ documentation on asserts.(Press Retry to debug the application)---------------------------Abort   Retry   Ignore   ---------------------------I checked line 138 of the file it mentions, and that is where it is dealing with the -- operator, so I'm pretty sure that's what's causing the problem.Does anyone know why this problem occurs and how to fix it?   
  If you decrement a vector iterator when it is the beginning iterator, that will cause in assertion error. Since you are just deleting everything in the vector, I don't know why you couldn't just do something like:  for (vector<Sprite*>::iterator siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++) delete (*siSprite);m_vSprites.clear();   ;  Thanks a lot that worked.  I didn't write the code, it was from a textbook.   ;  For future reference, the error occurred because when you call erase() on a iterator, all iterators to the vector are invalidated. std::vector::erase returns a valid iterator. With this, you could re-write the loop but it is more complex (you must not increment the iterator returned by erase or you will skip elements).One solution is to use boost::ptr_vector, if there are no ownership issues (i.e. the array owns the objects). Otherwise, a std::vector of boost::shared_ptr usually suffices (circular references excluded).A nice solution is to use some of the standard library algorithms.#include <algorithm>struct Deleter {    template< typename T >    void operator()( T *pointer ) { delete pointer; }};template< typename Container >void clearPtrContainer(Container &c){    std::for_each(c.begin(),c.end(),Deleter());    c.clear();}