我想用下一个代码解决的问题是将某个大小的整数数组旋转到某个数字。 例如,对于C++指针:数组的堆栈已损坏
int a[5] = {1,2,3,4,5}
其中size =5
,为shift = 2
结果必然是
{3,4,5,1,2}
这是我的功能
void rotate(int a[], int size, int shift)
{
for (int i = 0; i < shift%size; ++i) {
int *buffer = &a[0];
a = &a[1];
int l = *buffer;
a[size - 1] = l;
}
}
的输出是正确的,但有一个运行时异常
Stack around the variable 'a' was corrupted
的问题绝对是
a[size - 1] = l;
,但我不明白,究竟是什么错。
在此先感谢您的帮助。
就这么你知道,这已经存在了:'std :: rotate' – chris