Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

amemorex

Can I "delete []" a certain element of an array?

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

I know you can (and must) use delete on any new commands, but can you use it to delete an invididual element of an array? for instance, what would happen in the following code?
  
int *arr
arr = new int[5];
for(int i=0; i<5; i++) {
  arr[i] = i;
}

//then lets say i wanted to delete the 3rd element from

//the array..would this work..


delete [2] arr;
  
Would this do what I''m hoping? I''m sort of confused as to what it would do, since it''s a pointer..Would it remove the address value of the 2nd element..or the value..would the array become smaller?

Share this post


Link to post
Share on other sites
Advertisement
So when things like an STL vector let you remove a part of an vector (or add an element to it in a certain index of the vector), what does it do?

Does it create a whole new temporary variable, store the current array in it, delete the original, and copy the temporary one back into it (minus the deleted element)?

That seems really insufficient and slow, so how do they do it?

Share this post


Link to post
Share on other sites
quote:
Original post by amemorex
So when things like an STL vector let you remove a part of an vector (or add an element to it in a certain index of the vector), what does it do?

It''s implementation dependent. Most current implementations do exactly what you described, and yes it is slow and inefficient - but you''re supposed to know that! std::vector is not a "general-purpose" container, it is designed to allow random access in constant time and closely parallel C-style arrays. The price for that is that the container can not easily be reordered (including removing random elements).

If you frequently need to modify the controlled sequence, look into one of the other containers (particularly std::list, std::[multi]map and std::[multi]set.

However, consider the following example:

  
int **arr;
arr = new int *[5];
for(int n = 0; n < 5; ++n)
{
arr[n] = new int[5];
}
// Initialize arr

//

// Now you can delete a particular element of arr because it is

// dynamically allocated memory

delete arr[2]; // Valid. Just don''t access it after this, or

// until you reallocate it!



[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
By the way, delete[ number ] may compile (it compiles on VC6), although the number will probably be ignored. It's a holdover from the early days of C++ when you had to tell the compiler how many elements are in the array to be deleted. It looked like this, but note that it always deleted the whole array; the number just told the compiler how many destructors to call:


int* p = new int[ 5 ];
delete [ 5 ] p;


Oh, and Oluseyi's example needed the bracketed delete syntax:


delete [] arr[2];

--
Eric

Edited by - ekenslow on February 22, 2002 3:15:42 PM

Share this post


Link to post
Share on other sites

  • 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!