I'm looking for a more in depth explanation here, not just how to get working code. I know how to write much shorter code to erase elements. I wrote this test code here to pinpoint points of failure upon deletion. It would seem that not only the i iterator becomes invalid but also the .end() iterator... and that is interesting.
Why is that this works?
auto end = data.end();
for(auto i = data.begin(); i != end;)
{
if( (*i)->getName() == "apple" )
{
auto ti = i;
++ti;
end = data.end(); //above erase, works but not logical
data.erase(i);
i=ti;
}
else
{
++i;
end = data.end();
}
}
but this does not work?
auto end = data.end();
for(auto i = data.begin(); i != end;)
{
if( (*i)->getName() == "apple" )
{
auto ti = i;
++ti;
data.erase(i);
end = data.end(); //Bellow erase...more logical but crashes
i=ti;
}
else
{
++i;
end = data.end();
}
}
I'm guessing that there is dome implementation level issue here. Perhaps a compiler bug. Using GCC 4.8.2.
Aucun commentaire:
Enregistrer un commentaire