Moving arrays's value

closed account (oLC9216C)
Is here any way to move the value of arrays?
Like this,
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
and I want it become
a[0] = 1
a[1] = 3
a[2] = 4
May not be perfect but a quick solution.
Perhaps something like this...

1
2
3
4
// x is the array position to overlap (in your example, 1)
x = 1;
for(int i = x; i < a.size( ) < i++)
  a[i] = a[i + 1];


There are several ways to do the task. For example you can use C standard function memcpy

memcpy( a + 1, a + 2, 2 );

Or you can use standard algorithm std:;copy.

std:;copy( a + 2, a + 4, a + 1 );
Actually that for statement is incorrect. First you will be accessing your array out of bounds, and second you are missing the increment section, probably a typo.

Topic archived. No new replies allowed.