由N个元素在移动数组元素 [英] Shifting elements in array by N elements
问题描述
我有,其中每个元素是由4个连续值的阵列。更新后我通过4个值移动阵列接近尾声,并插入4个新值的开始。
I have an array where each 'element' is composed of 4 consecutive values. Upon update I move the array by 4 values towards the end and insert 4 new values in the beginning.
移:
int m = 4;
for (int i = _vsize - 1; i + 1 - m != 0; i--){
_varray[i] = std::move(_varray[i - m]);
}
插入:
memcpy(&_varray[0], glm::value_ptr(new_element), 4 * sizeof(float));
其中, new_element
的类型为 GLM :: vec4
包含上述4个新的值。
where new_element
is of type glm::vec4
containing said 4 new values.
关于如何提高这个有什么建议?
Any suggestions on how to improve this?
(现在我只是在一个元件移动,但希望是能够转向的灵活性,说的8倍,而不必把这种在一个循环中)
(Right now Im only shifting by one element, but want the flexibility of being able to shift say 8 times, without having to put this in a loop)
感谢您。
推荐答案
您可以尝试的 的std :: copy_backward
。要的范围内的值复制到另一个范围在同一容器中。由于范围重叠的和要复制到正确的你不能用普通的的std ::复制
,但必须使用的std :: copy_backward
代替。
You can try std::copy_backward
. You want to copy a range of values to another range in the same container. Since the ranges overlap and you are copying to the right you can't use regular std::copy
but must use std::copy_backward
instead.
int m = 4; // make this a multiple of your 'element' size
std::copy_backward(&_varray[0], &_varray[_vsize - m], &_varray[_vsize]);
还有 的std :: move_backward
但是这并不重要,因为你的浮动
值是不移动的。
这篇关于由N个元素在移动数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!