I was wondering if there is a possibility to shift a whole block in an array. I intend to delete one item in an array of dynamic length, and after I deleted it, I want the whole block to shift to the right. So far I do this element, by element, but this is not efficient. So I was wondering if there is a better solution.
Important: the order of the elements needs to be kept the same.
Using memmove
will probably be more efficient than doing an element by element copy as then you can take advantage of the fact you're doing a bulk move rather than lots of little moves (and compilers often provide highly optimized memmove
implementations), but that's about it. You need to move everything around in memory, so you're going to have to move it.
If you're doing this a lot with your arrays, it probably means you need a different data structure.