Delete an array element

Hello Programmers,
Good morning, I have a question in deleting an array element. Can anybody help me with the code and a little explanation.
Cheers,
Karl
Hi Karl,
If by deleting an array element do you mean deleting an object pointed by an element in an array of pointers:
 
delete myarray[elemen];

But if it is not an array of pointers and, by deleting an array element, you mean to shrink an array by deleting some element in the middle, that cannot be done in C++.
In this case, you could use a standard "list" or "vector" object, that support that:
http://cplusplus.com/vector::erase
Hi martinlb,
Thank you for the swift respones. I'm sorry for the ambiguity. What I mean is to delete the entry of the element, to blank the element.
Karl
So, you want the element to be there, but blank?
 
| 123 | 456 | 784 | 935 | 068 |


And you want to change it to:
 
| 123 | 456 |  0  | 935 | 068 |


If so, just set the value of that cell to 0 (or NULL).

If you mean to get rid of that element, you'd need to shift all the ones to the right of that element one to the left:
1
2
3
for (int i = index; i < /* length */; i++)
array[index] = array[index+1];
array[length-1] = 0;


Last edited on
Topic archived. No new replies allowed.