[問題] qsort
我要用內建的qsort來排陣列
但我最主要不是要排完的陣列
而是要它排後的index
以下此段程式在VC6可以跑
static int compare(const void* a, const void* b)
/* Helper function for sort. Previously, this was a nested function under
* sort, which is not allowed under ANSI C.
*/
{ const int i1 = *(const int*)a;
const int i2 = *(const int*)b;
const double term1 = sortdata[i1];
const double term2 = sortdata[i2];
if (term1 < term2) return -1;
if (term1 > term2) return +1;
return 0;
}
void CALL sort(int n, const double data[], int index[])
/* Sets up an index table given the data, such that data[index[]] is in
* increasing order. Sorting is done on the indices; the array data
* is unchanged.
*/
{ int i;
sortdata = data;
for (i = 0; i < n; i++) index[i] = i;
qsort(index, n, sizeof(int), compare);
}
但在VC.NET需要改寫
在我亂改一通後 變成
int Cemd1Dlg::compare(const void* a, const void* b)
{ const int i1 = *(const int*)a;
const int i2 = *(const int*)b;
int term1 = sortdata[i1];//問題
int term2 = sortdata[i2];//問題
if (term1 < term2) return -1;
if (term1 > term2) return +1;
return 0;
}
void Cemd1Dlg::CALL_sort(int n,int* data , int index[])
{ int i;
sortdata=new int [n];
for (i = 0; i < n; i++)
{
sortdata[i] = data[i];
index[i] = i;
}
qsort(index, n, sizeof(int), compare);
delete [] sortdata;
}
但仍然有問題,
error C2597: 參考非靜態成員'Cemd1Dlg::sortdata' 是不合法的
error C2109: 註標必須使用在陣列或指標型別上
不知道有沒有大大,可以給我一些指教呢?
謝謝^^~跪~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.129.77.9
推
08/23 18:58, , 1F
08/23 18:58, 1F
→
08/23 19:00, , 2F
08/23 19:00, 2F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章