J
Jim Langston
There's the thing about iterating though a map or vector when you may delete
one of the elements, where you simply assign the iterator to the map.erase()
statement or increment it if you don't. Well, I have this issue where I may
delete the map element in 3 different places:
for ( map_key_pcmissile::iterator mit = World.Missiles.begin(); mit !=
World.Missiles.end(); )
{
if ( /* Some Condition */ )
{
/*** Do some stuff and delete map element here ***/
}
else
{
if ( /* Some Condition */ )
{
for ( /* Iterate through vector */ )
{
if ( /* Some Condition */ )
{
if ( /* Some Condition */ )
{
/*** Do some stuff and delete map element here ***/
}
}
}
for ( /* Iterate through a different vector */ )
{
if ( /* Some Condition */ )
{
if ( /* Some Condition */ )
{
/*** Do some stuff and delete map element here ***/
}
}
}
}
++mit;
}
}
As you can see two of these are fairly heavily nested and so the ++mit; at
the end shouldn't occur if the map elements were deleted in any of the
above. Right now it's just checking one.
I'm wondering the best way to do this. The first obvious way is to simply
set some boolean flag if the element was deleted and only increment mit if
the flag is set to false. This is my usual solution in this type of
problem, but I don't tend to like it. It just seems not the Right Thing.
What would you do in this case? What is the Right Thing to do?
one of the elements, where you simply assign the iterator to the map.erase()
statement or increment it if you don't. Well, I have this issue where I may
delete the map element in 3 different places:
for ( map_key_pcmissile::iterator mit = World.Missiles.begin(); mit !=
World.Missiles.end(); )
{
if ( /* Some Condition */ )
{
/*** Do some stuff and delete map element here ***/
}
else
{
if ( /* Some Condition */ )
{
for ( /* Iterate through vector */ )
{
if ( /* Some Condition */ )
{
if ( /* Some Condition */ )
{
/*** Do some stuff and delete map element here ***/
}
}
}
for ( /* Iterate through a different vector */ )
{
if ( /* Some Condition */ )
{
if ( /* Some Condition */ )
{
/*** Do some stuff and delete map element here ***/
}
}
}
}
++mit;
}
}
As you can see two of these are fairly heavily nested and so the ++mit; at
the end shouldn't occur if the map elements were deleted in any of the
above. Right now it's just checking one.
I'm wondering the best way to do this. The first obvious way is to simply
set some boolean flag if the element was deleted and only increment mit if
the flag is set to false. This is my usual solution in this type of
problem, but I don't tend to like it. It just seems not the Right Thing.
What would you do in this case? What is the Right Thing to do?