delete the elements of an array

delete the elements of an array

Page 1 of 1

3 Replies - 1752 Views - Last Post: 19 May 2009 - 03:34 AM Rate Topic: -----

#1 abhijithblack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-May 09

delete the elements of an array

Posted 18 May 2009 - 10:06 PM

How to delete the element of an array
Is This A Good Question/Topic? 0
  • +

Replies To: delete the elements of an array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: delete the elements of an array

Posted 18 May 2009 - 10:29 PM

Can't, once an array is allocated, it's done. There are ways to fake around this, like using a pointer (allocating on the heap) instead of a traditional array, or using a container of some sort.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5355
  • View blog
  • Posts: 27,314
  • Joined: 10-May 07

Re: delete the elements of an array

Posted 19 May 2009 - 03:30 AM

Since an array is a predefined size, you can't delete the elements.

Once could dump the values, minus the ones you wish to delete, into a temporary array, then recalculate the size of the new array, trash the original variable, create a new one of the new size, & then re-assign the array again.

Sounds like a lot of work...
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: delete the elements of an array

Posted 19 May 2009 - 03:34 AM

Welcome to DIC!

Simulating deleting an element via 'move left'.

Assuming a 1D array.

Find the index of the element you want to delete.

Copy "index+1" into "index" (move the values one place to the left - overwriting the old value in the destination index with the value to its immediate right).

Replace the last 'real' element in the array with the null character '\0'.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1