Nested Loop that may potentially delete iterators of inner & outer vectors

This is a discussion on Nested Loop that may potentially delete iterators of inner & outer vectors within the Game Programming forums, part of the General Programming Boards category; Heya all, long time no see. Anyhow, I've got a situation where a vector of sprites* is checked against another ...

  1. #1
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56

    Nested Loop that may potentially delete iterators of inner & outer vectors

    Heya all, long time no see. Anyhow, I've got a situation where a vector of sprites* is checked against another vector of sprites*. If the pair being checked intersect, I'd like to delete them both. I can handle single-delete passes, but can't wrap my head around double-deletes, since the outer loop's iterator would also need to be incremented, but only if the inner iterator was incremented... anyhow, it's kicking my butt in practice.

    In the mean time, I simply set the sprite's lifetime to zero, so on the next loop, a different iteration catches and deletes the sprite.

    Code:
    for ( auto target = EnemyBullets.begin(); target != EnemyBullets.end(); ++target ) {
        for ( auto shot = Bullets.begin(); shot != Bullets.end(); ++shot ) {
            if (( *target )->getGlobalBounds().intersects(( *shot )->getGlobalBounds())) {
                ( *target )->setLifeTime( 0 );
                ( *shot )->setLifeTime( 0 );
            }
        }
    }
    The above works, but optimally first, I'd like to delete the two sprites directly. All my attempts segfault so far. Something like:

    Code:
    for (auto shot = Bullets.begin(); shot != Bullets.end(); ++shot) {
        for (auto target = EnemyBullets.begin(); target != EnemyBullets.end();) {
            if((*shot)->getGlobalBounds().intersects((*target)->getGlobalBounds())) {
                delete *shot;
                delete *target;
                shot = Bullets.erase(shot);
                target = EnemyBullets.erase(target);
            } else {
                ++target;
            }
        }
    }
    If I figure it out, I'll post an update.
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

  2. #2
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56
    Well, it's a little hacky, but here's the finished solution:
    Code:
                // Beginning of the code for bullet on bullet collisions.
                auto playerShotIter = PlayerBullets.begin();
                auto enemyShotIter = EnemyBullets.begin();
                bool bulletOnBulletCollision = false;
    
                while (playerShotIter != PlayerBullets.end())
                {
                    enemyShotIter = EnemyBullets.begin();
                    while (enemyShotIter != EnemyBullets.end())
                    {
                        if ((*playerShotIter)->getGlobalBounds().intersects((*enemyShotIter)->getGlobalBounds())) {
                            bulletOnBulletCollision = true;
                            delete *playerShotIter;
                            delete *enemyShotIter;
                            playerShotIter = PlayerBullets.erase( playerShotIter );
                            EnemyBullets.erase( enemyShotIter );
                            enemyShotIter = EnemyBullets.end();
                        } else {
                            ++enemyShotIter;
                        }
                    }
                    if (bulletOnBulletCollision) {
                        bulletOnBulletCollision = false;
                    } else {
                        ++playerShotIter;
                    }
                }
                // End of the code for bullet on bullet collisions.
    You can see I had to use an external flag (bulletOnBullerCollision) to signal to the outer loop that the outer iterator had already been incremented in an inner-loop deletion. Thanks for your time!

    Any comments, questions, suggestions or improvements are welcome! Please! =D
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 09:34 AM
  2. Vectors and Iterators
    By Trev614 in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2008, 10:30 AM
  3. vectors and iterators?
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2007, 11:39 AM
  4. <list> iterators with nested templates
    By dwylie in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2005, 12:13 PM
  5. vectors with same iterators
    By strickey in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2005, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21