Hi All,
I've been flummoxed by this bizarre behaviour and would like insight on how to get around it.
--Snip--
[code=c]
struct polygon
{
int i; int j;
}
void doStuff(vector< polygon>* myPolygons){
vector<polygon> tempPoly;
tempPoly.swap(* myPolygons)
vector<polygon> ::iterator itrV;
for(itrV=tempPo ly.begin(); itrV!=tempPoly. end();itrV++){ //1
polygon p = *(itrV);
if(p.i==-1){
itrV = tempPoly.erase( itrV); //0
}
tempPoly.swap(* myPolygons);
}
[/code]
--Snip--
Code fails at //1 (in VC2008, Debug mode) with an Assert Error in Vector around Line:117. The itrV apparently holds an invalid reference
Most other people make the mistake at //0 where they just call erase but don't use the returned iterator. I am doing that... so why does VC++ hate me?
Any inputs (excluding the idea that I can turn off the _SCL_SECURE_VAL IDATE_RANGE checking) I would like to understand what is the issue with the code and what is the right way to write erase loops for vectors.
Regards,
Abe
P.S. : myPolygons may start with 0,1,2... n elements and all might get deleted inside the loop
I've been flummoxed by this bizarre behaviour and would like insight on how to get around it.
--Snip--
[code=c]
struct polygon
{
int i; int j;
}
void doStuff(vector< polygon>* myPolygons){
vector<polygon> tempPoly;
tempPoly.swap(* myPolygons)
vector<polygon> ::iterator itrV;
for(itrV=tempPo ly.begin(); itrV!=tempPoly. end();itrV++){ //1
polygon p = *(itrV);
if(p.i==-1){
itrV = tempPoly.erase( itrV); //0
}
tempPoly.swap(* myPolygons);
}
[/code]
--Snip--
Code fails at //1 (in VC2008, Debug mode) with an Assert Error in Vector around Line:117. The itrV apparently holds an invalid reference
Most other people make the mistake at //0 where they just call erase but don't use the returned iterator. I am doing that... so why does VC++ hate me?
Any inputs (excluding the idea that I can turn off the _SCL_SECURE_VAL IDATE_RANGE checking) I would like to understand what is the issue with the code and what is the right way to write erase loops for vectors.
Regards,
Abe
P.S. : myPolygons may start with 0,1,2... n elements and all might get deleted inside the loop
Comment