Thread: About STL vector.erase

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    About STL vector.erase

    So I'm making a simple shooter game and I want to remove an element from my vector when it gets killed, my question is what is the best way to do it?

    I've worked out that I can delete an object no problem using the Vector.erase (Vector.begin () + Collided); method where Collided is the index of the object I want to remove. However would this leave a big gaps in memory? I use the vector.size () functions to work out how many objects I should check for collisions so will this be reduced when i kill of one of my units?

    Is this the best way to store the information about my units?

  2. #2
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    >However would this leave a big gaps in memory?
    No.

    >Is this the best way to store the information about my units?
    Yeah.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Ok thanks! Now a couple more questions:

    Why does my code crash when i try to do this:

    In my game loop I load waves of enemies like this:
    Code:
    void Logic::LoadWaves (void)
    {
    	BlankCounter++;
    	if ((BlankCounter > 50) && (WaveCounter == 1))
    	{
    		enemies.AddEnemyToVector (2);
    		WaveCounter =2;
    	}
    }
    BlankCounter is the number of frames being rendered and enemies is my storage class for my vector. The AddEnemyToVector looks like this:

    Code:
    void EnemyList::AddEnemyToVector (int NumberToSpawn)
    {
    		for (int i = 2; i < NumberToSpawn; i++)
    	{
    	venemyVector.push_back (new Enemy); // add new unit to array
    	venemyVector[i]->SpawnEnemy (i); // spawn enemy unit
    	}
    }
    Number to spawn is the number of enemies to spawn per wave, this all works fine, however when i try to remove them like this:

    Code:
    void EnemyList::KillEnemy (int Collided)
    {
    	ExplosionX = venemyVector[Collided]->GetLeft ();
    	ExplosionY = venemyVector[Collided]->GetTop ();
    	
    	Boom.LoadGFX (); // load graphics for explosion
    	Boom.Bang ((ExplosionX - 16), (ExplosionY - 16)); // run animation
    	venemyVector[Collided]->Die (Collided); //make the unit explode
    	venemyVector.erase (venemyVector.begin () + Collided);  // erase unit from vector
    }
    Its the two bold lines at the bottom which aren't right, it sometimes works when i kill the last spawning unit first but sometimes one of the units remains when i call delete on it, it doesn't move or anything, i just get the random explosion when it collides with my player. Anyone got any ideas what it could be? Do I need to swap to the end of the vector before i delete it or something? If so what is the code to do that?

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    When you use the vector throughout the code, do you access the vector elements sequentially or is there a need to access them in nonsequential order?

    If you're accessing sequentially, consider std::list<>. It'll almost certainly give you a performance boost, and if you have no need for nonsequential access, the only real benefit of vector<> over list<> is gone.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Quote Originally Posted by Cat View Post
    When you use the vector throughout the code, do you access the vector elements sequentially or is there a need to access them in nonsequential order?

    If you're accessing sequentially, consider std::list<>. It'll almost certainly give you a performance boost, and if you have no need for nonsequential access, the only real benefit of vector<> over list<> is gone.
    It has to be non-sequential since the game doesn't know which enemy the player will attack first, I think may be the problem in that the game as the units are being deleted then their numbers are changing? is there anyway to reorganise them somehow so that i only delete one from the end?
    Last edited by -pete-; 04-01-2007 at 01:10 AM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the order of data in the vector doesn't matter, you can swap the one you want to erase with the last item and pop_back the vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM