Vectors


PokeParadox

Founder of Pirate Games - Penjin Coder
Staff member
Joined
Dec 8, 2005
Messages
6,603
Age
40
Location
UK
Website
pokeparadox.itch.io
WEBSITE
https://github.com/pokeparadox
YOUTUBE
pokeparadox
Hi, I'm using vactors in my game. I have some creatures and they grow. I'm using vectors so they can grow indefinately.

I also want these creatures to shrink when another creature eats it... I could just pop_back() but I would rather remove the specific collided cell.

I'm sure there is a way to remove a specific array index in a vector, but I can't seem to figure out how :S

Any clues?


EDIT: I think I have it...
Code:
void creature::removeNode(int nodeIndex)
{
 	 for (int i = nodeIndex; i < nodes.size() - 1; i++)
 	 {
			nodes[i] = nodes[i+1];
	  }
	  nodes.pop_back();
}
 
Are they ordered? If not, this will operate in constant time instead of linear:
Code:
nodes[nodeIndex] = nodes.back();
nodes.pop_back();
Otherwise, nodes.erase(nodes.begin() + nodeIndex); should work.
 
If you are going to do a lot of insertion or deletion of elements in an array then use a list. Vectors will be too slow and more prone to odd crashes due to the implementation.
 
yaustar posted on Dec 10 2006 at 03:22 AM said:
If you are going to do a lot of insertion or deletion of elements in an array then use a list. Vectors will be too slow and more prone to odd crashes due to the implementation.

You do have a point...
 
Last edited by a moderator:
Back
Top