Saturday, May 9, 2009

C++ array question - "removing" an item from an array?

If I had an array in C++, with a capacity of 10 items but only 5 items in the array, and I wanted to remove the item located at position 2 in the array...how would I go about doing that?





I just don't know the syntax...I know that to delete an entire array, you would type:





delete [] array;





But for just deleting one item, you can't do something like:


delete array[2];





So how would I go about removing that item? Just set it to zero and move everything else over to fill the gap (putting a zero in the last occupied location of the array), use the null factor or what?

C++ array question - "removing" an item from an array?
Depending on what you want to acheive, resorting an array maybe a waste of time. Array are not the best data structure to insert and delete. Depending on how often you gonna do this operation and the size of your array, this might not be a good idea performence wise.





I usually give a null value to the value I want out and ignore it whenever I browse through the array. If you intend to add and delete stuff from your array very often, your maybe better off with a chained list.
Reply:First delete [] array is only to be used to delete an array created with new [] array. If you want to remove an element from a static array then you will have to copy the remaining elements to the one you wish to remove. For example





int array[10];


//Assume elements placed into array


for (int i(2); i %26lt; 9; ++i)


array[i] = array[i + 1];


array[9] = 0;





However C++ has something called the standard template library (STL), this is a library as defined by ISO/ANSI as a part of C++, it contains template funcitons and containers, by far the most used container is vector which is a variable length array. The previous example would become





std::vector%26lt;int%26gt; array;


//Assume elements placed into the vector


array.erase(array.begin() + 2);





This is shorter and easier to use, if you want to know more about the STL then read Accelerated C++ by Andrew Koeing and Barberer E. Moo.
Reply:Just a couple of quick notes. The first poster is correct in saying that you can just move each element forward one space and overwrite the element you wish to delete. However, there is a caveat. If the element you wish to delete is a pointer to something that's been allocated using the new operator, you should explicitly delete it first before you overwrite it.





The second poster has a good point, although chained lists are more normally called linked lists.





Third, you can implement the 1st posters suggestion using 1 line using the movemem() function, which would be faster. But it's easier to screw that up, so I wouldn't recommend it unless you need every bit of speed.
Reply:While the most popular method is to move items ahead when deleting (as presented above), there is another way. By moving all the items down in the array you would not have to worry about overflowing your array when using a more familiar looking for loop.





//code begin





int removeIndex = 2;


int arrayLength = 10;





for(int i = removeIndex + 1; i %26lt; arrayLength; i++)


{


array[i-1] = array[i];


}


arrayLength--;





//end code





The other information about using delete and checking for invalid removal indexes is applicable, but this code does the trick as well.
Reply:Hello





You don't need to set it to zero. Do a FOR function that goes from the desired deletion point to the end that moves a[n+1] into n. So just move all the positions from the desired point one position back. Do a function and appeal it.





function delarraypos (int n)


{ cin%26gt;%26gt;n; //desired position


for (i=n;i%26lt;=m;i++) //m is array length


a[n]=a[n+1]


}
Reply:Mihai is largely correct, but here are a few more things to be aware of if you are to use his code:





function delarraypos (int n)


{ cin%26gt;%26gt;n; //desired position


for (i=n;i%26lt;=m;i++) //m is array length


a[n]=a[n+1]


}





1. The second element of an array is not array[2], so make sure that you either decrement n or pass in the proper index of the item you want to delete.





2. If the array length is m, the indices are 0 through m-1, so you don't want to go until m. Also, since you are going to set a[n]=a[n+1], going all the way to the end will make a[n+1] go past the end of the array, so you really only want to iterate to m-2.





3. The last element of the array will be left as it is if you do this. If you want to indicate unused elements of the array with 0, then you need to set array[m-1]=0. This will be important if the element to be deleted is at the end of the array, where there is nothing to move on top of it, if you want to make sure that it is not left with a non-0 value.





4. If you are concerned with a memory leak due to the array being populated with objects that have been "newed" elsewhere, then you still need to do a "delete array[2]" before rearranging the array to make sure the memory is freed up. Your example of setting the unused elements to 0 implies that this is an array of integers. If that is the case, then there is no need to delete the integer.





5. If the array's current length "m" is being stored somewhere, you will want to decrement it. (By "current lenngth," I mean how much of the array is filled as opposed to how much memory has been allocated for the array.)





6. Finally, unless you can be sure that the n value being passed in is good, it is good practice to check that n%26gt;=0 and n%26lt;m, and either throw an exception or assert out. If n is too big, this procedure will mess seriously with your memory in a way that will be hard to track down.





Hope this helps.


No comments:

Post a Comment