Answer the question
In order to leave comments, you need to log in
Fastest way to shift array elements by one position?
There is an array of data: long long *Data=new long long[DATA_COUNT];
DATA_COUNT can be thousands and tens of thousands.
It is necessary at each iteration to shift the array elements by one position to the left (overwriting the very first element).
For example:
Before - 0, 1, 2, 3, 4, 5
Now - 1, 2, 3, 4, 5, X - where X is the location for the next (newer) data.
I do like this:
memmove (&Data[0], &Data[1], (DATA_COUNT-1)*sizeof(long long));
Answer the question
In order to leave comments, you need to log in
In fact, such a structure is called a queue (FIFO) and is implemented on the basis of two pointers - reading (beginning of the queue) and writing (end of the queue).
It's much faster to change the pointer to the "beginning" of the array.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question