[try Beta version]
Not logged in

 
How to completely remove one element of an array?

Aug 2, 2009 at 9:27am
Let's say I have an array:
 
int ARRAY[] = [3,1,4,1,5,9];


Now lets pretend I want to get rid of an element completely, say number 2, leaving me with

[3,1,1,5,9]

How would I do this, generally speaking, for any element of an array?

EDIT:
I just had an idea:
I could rotate the array about element number 2 to get

[3,1,1,5,9,4]

and then (somehow?) delete the last element... if that's possible...
Last edited on Aug 2, 2009 at 9:34am
Aug 2, 2009 at 9:50am
You should use braces instead of square brackets: int ARRAY[] = {3,1,4,1,5,9};.

The size of an array is fixed and cannot be changed. You can imitate this if you allocate the memory on the heap, create another array of an appropriate size and copy the necessary information, then call delete on the original one.

If you want more convenient ways to insert/remove data, condsider using a std::vector.
Aug 2, 2009 at 11:56am
If you want more convenient ways to insert/remove data, condsider using a std::vector.
a std::vector is much like a dynamic array for insertion/removal of elements a std::list is better
http://www.cplusplus.com/reference/stl/list/
Topic archived. No new replies allowed.