Why is this piece of code giving me the error : Vector iterators incompatible
This piece of code was traced back to the Rogue Wave file tpordvec.h
std::vector<T*> v;
const T* a // Where T is a template Class
for (std::vector<T*>::iterator p = v.begin(); p != v.end(); p++)
{
if (**p == *a)
{
T* temp = *p;
if ( v.erase(p) == v.end()) //ASSERTION ERROR HERE
return NULL;
return temp;
}
}
http://en.cppreference.com/w/cpp/container/vector/erase
Hence if the
vector.end()is evaluated before thevector.erase()andvector.erase()really erases and by doing so invalidates the iterators tillend(), the call tooperator==()will be between two incompatible iterators.Something like this would be better: