# Erase element at index in vector

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

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

##### Share on other sites
Quote:
 Original post by Gage64Why 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 virvelvindendo 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 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 on other sites
Quote:
 Original post by virvelvindenWith 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 on other sites
Quote:
Original post by dmatter
Quote:
 Original post by virvelvindenWith 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 CmpDevGenerally 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 on other sites
Well I suppose it depends on the situation, you could use a id created by lua itself ie a Lua reference or use your own defined id, storing the information in an unique associate container such as std::map.

##### 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 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...

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633680
• Total Posts
3013304
×