Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   list iterator (http://www.velocityreviews.com/forums/t291299-list-iterator.html)

Flwz 05-23-2005 09:18 PM

list iterator
 
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 !

Flwz 05-23-2005 09:29 PM

Re: list iterator
 
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 ; )

Victor Bazarov 05-24-2005 01:54 PM

Re: list iterator
 
Flwz wrote:
> 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


All times are GMT. The time now is 07:26 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.