move values in an array?

header
int my_array[20];

init my_array
1
2
for(int i=0; i<21; i++)
my_array[i] = 100; //value 100 means no value set in array 


example;
all values in my_array are set to 60, but positions [2], [5] and [6] in the array are empty (have a value of 100), how do i organize the array so that the empty values are at (in this case) [18],[19],[20] and then move the positions with data down the necessary positions to fill the empty value positions?

what are the best methods to doing this?




Not using an array and using a list instead.
Not using an array and using a list instead.


That defeats the purpose of this thread. List is probably the best way to go however, but i would still like to know the methods for moving array values
For starters, you are exceeding your array bounds.

1
2
int my_array[20];
my_array[20] = 100; // OUT OF BOUNDS -- last valid index is [19], not [20] 


Anyway, you can use <algorithm>'s remove function:

http://cplusplus.com/reference/algorithm/remove/

example:

1
2
3
4
int* array_end = my_array + 20;
int* p = std::remove(my_array,array_end,100);
for(; p != array_end; ++p)
  *p = 100;
Topic archived. No new replies allowed.