Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 下面的代码是关于按某个值d旋转已排序的array_C++_Arrays_Loops_For Loop_Rotation - Fatal编程技术网

C++ 下面的代码是关于按某个值d旋转已排序的array

C++ 下面的代码是关于按某个值d旋转已排序的array,c++,arrays,loops,for-loop,rotation,C++,Arrays,Loops,For Loop,Rotation,我的代码不起作用,它产生了意外的结果。该代码是关于使用临时数组进行数组旋转的。函数“rotate”旋转数组,而函数printary打印数组。在主函数中,两个函数都被调用。然后是“你好”的cout。如果“ELLOLOHELLO”,则为总输出。为什么我会得到这个输出。谢谢 #include<iostream> using namespace std; void rotate(int arr[],int d, int n){ int temp[d]; for(int i =0;i&

我的代码不起作用,它产生了意外的结果。该代码是关于使用临时数组进行数组旋转的。函数“rotate”旋转数组,而函数printary打印数组。在主函数中,两个函数都被调用。然后是“你好”的cout。如果“ELLOLOHELLO”,则为总输出。为什么我会得到这个输出。谢谢

 #include<iostream>
 using namespace std;

 void rotate(int arr[],int d, int n){
int temp[d];
for(int i =0;i<d;i++){
    temp[i]=arr[i];

}
for(int i = 0;i<n-d;i++){
    arr[i] = arr[i+d];
}
for(int i =0 ;i < d;i++)
{
    temp[i]= arr[n-d+i];

}

}
void printArray(int arr[],int size){
for(int i =0;i<size;i++)
{

    cout<<arr[i]+" " ;
}
}
int main()
{

int arr[10] = {0,1,2,3,4,5,6,7,8,9};
rotate(arr,3,10);
printArray(arr,10);
cout <<"hello";
}
;
#包括
使用名称空间std;
无效旋转(int arr[],int d,int n){
内部温度[d];

对于(int i=0;iChange
cout的启动器,可变长度数组

void rotate(int arr[],int d, int n){
    int temp[d];
    //...

不是标准C++特性。或者使用辅助标准容器,例如STD::vector或STD::List或动态分配数组。< /P> 在函数的最后一个循环中

 void rotate(int arr[],int d, int n){
int temp[d];
for(int i =0;i<d;i++){
    temp[i]=arr[i];

}
for(int i = 0;i<n-d;i++){
    arr[i] = arr[i+d];
}
for(int i =0 ;i < d;i++)
{
    temp[i]= arr[n-d+i];

}
}
这里使用了指针算法。也就是说,字符串文字
隐式转换为指向其第一个元素的指针,数值
arr[i]
用作此指针的偏移量。请改为写入

cout<<arr[i] << " " ;

cout
cout
int temp[d];
这违反了标准。请使用std::vector@theWiseBro
“”
不是空格的ascii值,但您似乎已经发现了问题所在。@user253751是的。谢谢。这里的问题是您在使用
C++
时使用
C
样式进行编码。请注意,有一个现成的算法可以解决您的问题。
arr[i]+" " 
cout<<arr[i] << " " ;