Sign in to follow this  
virvelvinden

Erase element at index in vector

Recommended Posts

Hi, With a vector of ints you could call
intVector.erase(intVector.begin() + index);

...but what is the best way to do this with a vector of objects?
vector<testClass> testVector;

testVector.erase(???);

do I have to iterate through all the previous elements? thanks for any help!

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Why does it matter what's stored in the vector? You give erase() an iterator, and it removes the element at that iterator.

yes, and
Quote:
Original post by virvelvinden
do I have to iterate through all the previous elements?

...or is there a way to set the iterator to testVector[4] (for example) directly?

Share this post


Link to post
Share on other sites
Generally knowing the index of an element into a vector is bad mojo; unless you have just found the index via a find function in which case why not use an iterator as Gage64 pointed out. As for "but what is the best way to do this with a vector of objects?" define best, if you mean optimal then the old swap and pop trick is best.

Share this post


Link to post
Share on other sites
Quote:
Original post by virvelvinden
With a vector of ints you could call

*** Source Snippet Removed ***

That works not because the vector is storing integers but because a random access iterator can be incremented by adding an integer. It doesn't matter matter what the vector's data type is, a vector's iterator can still be incremented by adding an integer.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
Quote:
Original post by virvelvinden
With a vector of ints you could call

*** Source Snippet Removed ***

That works not because the vector is storing integers but because a random access iterator can be incremented by adding an integer. It doesn't matter matter what the vector's data type is, a vector's iterator can still be incremented by adding an integer.


whoa, it actually did work. i was sure i got an error while trying this yesterday, it must have been the intertubes... :)

Quote:
Original post by CmpDev
Generally knowing the index of an element into a vector is bad mojo

I know, the reason i still go with it is that i'm going with Lua for scripting my game, and need some kind of temporary ID to my objects to perform actions on them... Of course this approach makes the ID change when erasing objects, it may be better to give each object a unique ID upon creation. is there an easy way to

testVector.getObjectWhereIdEqualsTo(int id);

when using unique ID:s? (Like, extracting all the objects from the vector with this ID)

Share this post


Link to post
Share on other sites
When using a vector there are ways around the problem of indices changing, like for example not erasing the element and instead flagging it as dead. Maybe the vector just contains pointers to objects so you can delete the object and nullify the pointer but leave it in the vector.

The usual way to associate a key (ID) with a value (your objects) is using an associative container, like a map:

std::map<int, Entity> entities;

entities[5].attack();
entities[2].jump();



Of course, with a map, the key doesn't have to be an integer:

std::map<std::string, Entity> entities;

entities["Fred"].attack();
entities["George"].jump();



You might choose to think of a vector as a special case of an associative container where the key is always an integer.

Share this post


Link to post
Share on other sites
Of course, maps should work. I've used maps in java and it was pretty straight forward. As for Lua references, I will look into it. I hardly know any Lua at all, but the concept seems nice...

Anyway, thanks for your help and for taking your time!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this