Deleting Objects from an Array

Started by
6 comments, last by 21st Century Moose 12 years, 2 months ago
Is it possible to delete a certain object from an array ?

#include<iostream>
using namespace std;

class animal
{
//random stuff here
}

//following stuff

int main()
{
animal animalGroup = new animal[5]

//already initilized all the animals already from the array

delete animalGroup[3]; // say if I wanted to delete the third one only

return 0;
}


Is it possible if I wanted to do so ?
Advertisement
Not with a normal array made with new. You can do this with std::vectors, or any stl container really, though. They also allow you to add new items to the array. http://www.cplusplus.com/reference/stl/vector/erase/
As it is now, no you cannot. However if you would use an array of pointers to animal objects, then you could, but it can get a little bit complicated, so better use STL as zacaj suggested if you are using c++.

here is a non-stl example:

class animal
{
public:
animal(){}
int a;
};
int _tmain(int argc, _TCHAR* argv[])
{
animal ** ptr;
ptr = new animal*[5];
for(int i = 0; i < 5; i++)
{
ptr = new animal();
}
delete ptr[2];
delete[] ptr;
return 0;
}


Notice that after you delete ptr[2], if you try to read that value you will get an exception. Maybe you could explain why you need to do this?
Yes, vectors are your friend.
If you are going to be deleting/moving data often, arrays and vectors are a bad choice. A vector will get the job done but will perform poorly when deleting arbitrary elements. If performance is a consideration, use list instead: http://www.cplusplus.com/reference/stl/list/.
Quit screwin' around! - Brock Samson
That's a gross over-generalization. Deleting arbitrary elements from a vector has a worse asymptotic complexity than a list, but the constant terms of list deletion and general use cases such as iteration end up being worse. If element order doesn't matter then you can use swap and pop with the vector which has the same asymptotic complexity as a list deletion and generally better constant terms. You just can't say that using a list always has better performance than a vector, even if you limit the usage case to arbitrary element deletion. As always, the profiler is your friend.
Well, STL in general has some problems here and there, but overall is a good choice if you are not trying to do wild things with it or you aren't targeting some mobile platforms that don't have a STL implementation to go along their C++ implementation. Always use what seems handy at first to get the job done, then if you identify a problem, as SiCrane said, profile your application and fix the bottlenecks. Using anything but vector in your case would be an early optimization in my opinion.
If all you want is to delete an item, and if order doesn't matter, just swap the item you want to delete with the last item in the array, then delete the last one (which will be a much more trivial case than deleting from a midpoint).

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement