C ++:“ arr”和“ arr []”之间有什么区别

时间:2018-09-13 11:30:14

标签: c++ arrays

我正在研究的程序的一部分是对一个整数数组进行排序,因此我使用了最简单的算法选择排序。当我在线检查算法时,发现该函数的参数为​​selectionSort(int arr[], int n),其中 n 是数组的大小,稍后将输入;因此,当我在main()函数中调用该函数时,我按如下所示selectionSort(arr[n], n)对其进行了调用,并尝试打印该数组只是为了检查该函数是否有效-只是不排序就打印了该数组。然后我只去掉了方括号,就很好了。

这是代码:

void swap(int *xp, int *yp){
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void selectionSort(int arr[], int n){
    int i, j, min_idx;
    for (i = 0; i < n-1; i++)
    {
        min_idx = i;
        for (j = i+1; j < n; j++)
          if (arr[j] < arr[min_idx])
            min_idx = j;
        swap(&arr[min_idx], &arr[i]);
    }
}
int main(){
    int n; cin>>n;
    int arr[n];
    for(int i = 0; i < n; i++){
        cin>>arr[i];               //fill in the array
    }
    selectionSort(arr, n);         //Here is the point, if I write arr[]
    for(int i = 0; i < n; i++){cout<<arr[i];}
}

Selection Sort reference

The Problem I am working on on Code Forces

0 个答案:

没有答案
相关问题