Vector Iterator Incompatible
-
I am getting a run time error with the code below. Trying to use a static iterator to reduce the number of calls to search a vector. This is a method of a class that contains a vector of curves (so _vector is part of the class.) It runs correctly the first few times called, but the first time the static iterator doesn't point to the correct curve and _vector should be searched, I get a runtime error "Incompatible Vector Iterators" calling the == operator. I can't really tell what the error is. If it is a const versus non-const or it is creating copies of the vector somewhere. Is there a better way to do this? Thanks Mark
bool findpt(pt_tag<_Ty>* s)
{
if (_vector.empty())
return false;static vector< curve<\_Ty> >::iterator i = \_vector.end(); if (i == \_vector.end()) <-- RUN TIME ERROR HERE i = std::lower\_bound( \_vector.begin(), \_vector.end(), s->\_dtEffDate ); else { if ((i.\_Ptr->getdate() != s->\_dtEffDate)) i = std::lower\_bound( \_vector.begin(), \_vector.end(), s->\_dtEffDate ); } if ((i == \_vector.end()) || (s->\_dtEffDate != (\*i).\_date)) return false; cpoint<\_Ty> c; c.setdate( s->\_dtFwdDate ); if (i.\_Ptr->interpolate( &c ) == true) { s->\_valid = true; s->\_value = c.getvalue(); return true; } return false;
}
-
I am getting a run time error with the code below. Trying to use a static iterator to reduce the number of calls to search a vector. This is a method of a class that contains a vector of curves (so _vector is part of the class.) It runs correctly the first few times called, but the first time the static iterator doesn't point to the correct curve and _vector should be searched, I get a runtime error "Incompatible Vector Iterators" calling the == operator. I can't really tell what the error is. If it is a const versus non-const or it is creating copies of the vector somewhere. Is there a better way to do this? Thanks Mark
bool findpt(pt_tag<_Ty>* s)
{
if (_vector.empty())
return false;static vector< curve<\_Ty> >::iterator i = \_vector.end(); if (i == \_vector.end()) <-- RUN TIME ERROR HERE i = std::lower\_bound( \_vector.begin(), \_vector.end(), s->\_dtEffDate ); else { if ((i.\_Ptr->getdate() != s->\_dtEffDate)) i = std::lower\_bound( \_vector.begin(), \_vector.end(), s->\_dtEffDate ); } if ((i == \_vector.end()) || (s->\_dtEffDate != (\*i).\_date)) return false; cpoint<\_Ty> c; c.setdate( s->\_dtFwdDate ); if (i.\_Ptr->interpolate( &c ) == true) { s->\_valid = true; s->\_value = c.getvalue(); return true; } return false;
}
are you modifying the vector between calls to this function? if so, that might (will, probably) invalidate 'i'.
-
are you modifying the vector between calls to this function? if so, that might (will, probably) invalidate 'i'.
I am canning this routine as the vector may end up being modified. Thx for the input.