Moving array values around

I have an array with a lot of numbers in it and based upon certain circumstances I need to eliminate a variable within that array and then shift/move/copy all values one spot over (shifting all numbers from, say, array[400] and up, to all be shifted to encompass array[399] through array[998]. I am currently doing this with a for loop, but it seems rather time consuming and was hoping there was a command I could use to expedite this process.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
iamarray[1000];

for (n = 0; n <= 999; n++)
{

if (iamarray[n] == 395)
{
for (n1 = n, n2 = n + 1; n2 <= 999; n1++, n2++)
{
iamarray[n1] = iamanarray[n2];
}
iamarray[999] = 9999  // A number not normally occurring in the elements of the array to show that it is the equivalent of "null" for me as, "0" is an occurring number.
}

}

My actual arrays are thousands long and have to shift many times. I have just put up some code that resembles what I'm doing. To put up my actual code would be huge and confusing. Just looking for a command or something that can do this, ideally, much quicker. Thanks.
Use std::rotate:

1
2
3
4
int array[] = {1, 2, 3, 4, 5, 6};

std::rotate(array+1, array+2, array+5); //rotate 2-6 over once (puts 2 at the end)
array[5] = -1 //or whatever 
Last edited on
I'm confused by that, but I noticed I left something out. In my example code, I eliminated the element in the array with a value of "395" and then shifted all subsequent numbers (from that point in the array on, down one, so that instead of reading something like the following for "array[200] - array[206]"

"1, 4, 9, 395, 9, 10, 11"

it instead would read

"1, 4, 9, 9, 10, 11, 9999"

with the final number being a "null" or non occurring number in the array and I don't see how what you've referenced would enabled me to do that.
Last edited on
Topic archived. No new replies allowed.