6 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個贊
這里有個例子, 你可以把參數(shù) sting換成的你的類型 stud然后相應(yīng)用學(xué)號比較。
private static int CompareDinosByLength(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
參數(shù)就填這個方法名。

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個贊
s 實(shí)際 是一個二維指針,直接傳到sort里類型不符,sort只接受一維指針作為參數(shù),傳二維指針是無論如何也達(dá)不到目的的,要使一個函數(shù)能接收二維指針,必須指明大小,如 void example(int a[][5]) 而sort當(dāng)初這個接口設(shè)計(jì)的時候就沒有考慮到二維指針,所以這里是行不通的。 PS:既然已經(jīng)用了c++,C的一些東西能扔就扔,多用string,vector,list,還有些智能指針。少用些直接的指針,c++的指針出手一定是重量級的。

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個贊
sort(s,s+n,cmp);
傳入的參數(shù)是 s,可以理解為 str *,即 指向字符串的指針
使用 sort 需要處理幾個問題:
1. ++操作;
2. 比較;
3. 賦值
你對于第三個沒有處理,
假定有變量
str a,b;
a = b; // 這樣是不可以的
- 6 回答
- 0 關(guān)注
- 1702 瀏覽
添加回答
舉報(bào)