Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How bad is a goto statement?

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
How bad is a goto statement?
TeamTerradactyl
Member #7,733
September 2006
avatar

FMC said:

for (fl = snow.begin(); fl!=snow.end(); fl++)
{
  if (fl->dead())
  {
    snow.erase(fl);
    fl--; // <-- I think this is invalid
  }
}

If I'm not mistaken, the vector iterator only has the ++ operator defined; not the --, so you could not decrement your current position...

You got C++? So if I got B-, I'm smarter than you?
He got hit so hard, he fell off the internet.
SOH CAH TOA: "some old hippie caught another hippie tripping on acid" - Jeff Bernard

Dustin Dettmer
Member #3,935
October 2003
avatar

Quote:

//fl...ake is a vector iterator of the same type as snow
//snow is a vector
for(fl = snow.begin(); fl!=snow.end(); fl++){
if(fl->dead()){
snow.erase(fl);
fl--;
}
}

Is this wrong or bug prone?

Yes. This is what you want to do.

//fl...ake is a vector iterator of the same type as snow
//snow is a vector
for(fl = snow.begin(); fl!=snow.end(); ){
    if(fl->dead()){
        fl = snow.erase(fl);
    }
    else fl++;
}

Bob
Free Market Evangelist
September 2000
avatar

How about this?

snow.erase(std::remove_if(snow.begin(), snow.end(), std::mem_fun(&snow_type::dead)),
           snow.end());

(Untested code)

--
- Bob
[ Webpage | Allegro FAQ | Coding Tricks ]
"Oh, you want to do actual work. In that case, avoid the GameCube at all costs!" - Me

Dustin Dettmer
Member #3,935
October 2003
avatar

FMC
Member #4,431
March 2004
avatar

Thanks for the head-ups!

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

 1   2   3   4 


Go to: