0

我收到此错误:

prog.cpp:1:5: error: expected unqualified-id before ‘[’ token

形成此代码:

int [] quick_srt(int array[], int low, int n){

请问是这个问题吗?

编辑

int [] quick_srt(int array[], int low, int n){
int lo = low;
int hi = n;
comp++;
if (lo >= n){ // lo is greater then or equal to n
    return array;
}
int mid = array[(lo + hi) / 2];  //Using mid as pivot
comp++;
while (lo < hi){ //lo is less then hi

    comp++;
    while (lo < hi && array[lo] < mid){ //lo less than hi AND array[lo] less than mid
        lo++;
        comp++;
    }

    comp++;
    while (lo < hi && array[hi] > mid) {//lo less than hi AND array[lo] greater than mid
        hi--;
        comp++;
    }

    comp++; //for if
    comp++; //for else
    if(array[lo] == array[hi]){
    break; //for duplicate items
    }
    else if (lo < hi) { // less than
        int T = array[lo];
        array[lo] = array[hi];
        array[hi] = T;
        swaps++;
    }
    comp++;
}
comp++;
if (hi < lo) { //hi is less than lo
    int T = hi;
    hi = lo;
    lo = T;
}
quick_srt(array, low, lo); //recrusie call
quick_srt(array, lo == low ? lo+1 : lo, n); //re-call, if lo = low, increment lo else pass lo and n
return array;
  }
4

3 回答 3

2

您不能从函数返回数组。参数列表中的int array[]只是 的语法糖int *array,返回值没有类似物。如果你想返回一个指针,你需要明确地这样做:

int *quick_srt(int array[], int low, int n);

或者,等效地:

int *quick_srt(int *array, int low, int n);
于 2013-09-25T22:58:23.717 回答
2

int []仅对函数参数有效。我建议你这样做:

void quick_srt(std::vector<int>& array, int low)

你不需要n论证。使用 `array.size() 代替。

于 2013-09-25T23:00:58.723 回答
0

另一种方法是使用 void quick_srt(int * array, int low)

我建议使用矢量更容易

于 2013-09-26T06:58:53.937 回答