例如,[1,2,3,4,5]將變成[5,1,2,3,4]如何只使用指針將一個數組中的每個元素向右移動?
我不能使用額外的數組,只能使用索引遍歷。我也可以使用整數來存儲值,但這似乎沒有幫助。
這是我嘗試不工作:
void shiftonetoright(int arr[], int n){
int *ptr1 = arr;
int s1;
while(n>0)
{
ptr1++;
s1 =*ptr1;
*ptr1 =s1;
n--;
}
}
使用'的std ::旋轉(ARR,ARR + 1,ARR + N)'。 –
這與你以前的問題(接受答案)有顯着不同嗎?順便說一句's1 = * ptr1; * ptr1 = s1;'在你的代碼中沒有多大意義... – Blastfurnace
['std :: rotate()'](http://en.cppreference.com/w/cpp/algorithm/rotate)確實是要走的路。此外,你可能會發現Sean Parent的[C++調料](https://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning)在這個話題上特別提供了信息; – 865719