Jump to content
  • Advertisement
Sign in to follow this  
virvelvinden

Erase element at index in vector

This topic is 3733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Why does it matter what's stored in the vector? You give erase() an iterator, and it removes the element at that iterator.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!