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

Started by
3 comments, last by amemorex 22 years, 1 month ago
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?
Advertisement
It wont compile. You have to delete the whole array.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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?
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 memorydelete 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!
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
-- Eric

This topic is closed to new replies.

Advertisement