Friday, July 31, 2009

How to delete an empty slot in an array in c++?

about the only way is:





char myarray[10] = "abcdefghi";


int count;





say you want to get rid of (or delete) the g in the above array





for (count = 6; count %26lt; 8; count++)


___myarray[count] = myarray[count + 1];





now myarray contains "abcdefhi"





just a simple example of how to do it.

How to delete an empty slot in an array in c++?
Element that has been created as in an Array can’t be deleted from the memory. It’s not like a train that you remove any block from the train line.





You want to remove it, why? You want to safe memory? If yes, try to use linked list instead of using array. In linked list, it likes an Array but each node or each element is declared when to be used and you can remove it when you don’t want anymore.





Linked List is not a program; it’s one kind of data structure. Array is one kind of data structure and Linked List is the better data structure.





You need some code, see this.
Reply:please can you provide more details, what do you mean by empty slot, does it contain 0 or some junk value. what is the domain of your array actually.
Reply:You can't "delete" an element of an array. What you want is a VECTOR, here's the syntax:


#include %26lt;vector%26gt;


vector%26lt;int%26gt; myVector();


// from now out you can use it like an array e.g myVector[5]=0


//to "delete" an element use myVector.erase(position);


No comments:

Post a Comment