list iterator

F

Flwz

There's something I don't quite understand, sorry if that sound stupid
or whatever ; )

why is :

for( it = Flare::flareList.begin() ; it != Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
}

not working ? Since erase is supposed to return a valid iterator or the
end of the list... I made a workaround but it doens't look so cool to me


bool through;
do
{
through = true;

for( it = Flare::flareList.begin() ; it != Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
through = false;
break;
}
}
while (!through);


That works but it's neither efficient nor pretty, so if anyone could
explain me what's going on in the first case and a better work around ;)
thanks !
 
F

Flwz

Flwz a écrit :
There's something I don't quite understand, sorry if that sound stupid
or whatever ; )

why is :

for( it = Flare::flareList.begin() ; it !=
Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
}

not working ? Since erase is supposed to return a valid iterator or the
end of the list... I made a workaround but it doens't look so cool to me

Ok alright, I found out (why is it always just after I post ? ;)
if it is at the end of the list after the erase, it still does it++
before checking the end for statement, so it obviously will crash ; )
 
V

Victor Bazarov

Flwz said:
There's something I don't quite understand, sorry if that sound stupid
or whatever ; )

why is :

for( it = Flare::flareList.begin() ; it !=
Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);

You assign the value here and also do it++ after every iteration. You
should only do either of them.
// Flare::flareList.push_back( Flare());
}

not working ? Since erase is supposed to return a valid iterator or the
end of the list... I made a workaround but it doens't look so cool to me


bool through;
do
{
through = true;

for( it = Flare::flareList.begin() ; it !=
Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
through = false;
break;
}
}
while (!through);


That works but it's neither efficient nor pretty, so if anyone could
explain me what's going on in the first case and a better work around ;)
thanks !

for (it = Flare::flareList.begin(); it != Flare::fllareList.end(); )
{
if (it->isWorn())
it = Flare::flareList.erase(it);
else
++it;
}

should do it.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top