Sign in to follow this  

Deleting Objects from an Array

This topic is 2118 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

Is it possible to delete a certain object from an array ?
[code]
#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;
}
[/code]

Is it possible if I wanted to do so ?

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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:
[CODE]
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[i] = new animal();
}
delete ptr[2];
delete[] ptr;
return 0;
}
[/CODE]

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?

Share this post


Link to post
Share on other sites
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. [i]If[/i] performance is a consideration, use list instead: [url="http://www.cplusplus.com/reference/stl/list/"]http://www.cplusplus.com/reference/stl/list/[/url].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

This topic is 2118 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.

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