cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : Delete an array element
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  Delete an array element

Karlinods (20)
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
|
martinlb (36)
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
|
Karlinods (20)
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
|
AzraelUK (57)
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

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us