6 回答

TA贡献1777条经验 获得超10个赞
这里有个例子, 你可以把参数 sting换成的你的类型 stud然后相应用学号比较。
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);
}
}
}
参数就填这个方法名。

TA贡献1995条经验 获得超2个赞
s 实际 是一个二维指针,直接传到sort里类型不符,sort只接受一维指针作为参数,传二维指针是无论如何也达不到目的的,要使一个函数能接收二维指针,必须指明大小,如 void example(int a[][5]) 而sort当初这个接口设计的时候就没有考虑到二维指针,所以这里是行不通的。 PS:既然已经用了c++,C的一些东西能扔就扔,多用string,vector,list,还有些智能指针。少用些直接的指针,c++的指针出手一定是重量级的。

TA贡献1895条经验 获得超7个赞
sort(s,s+n,cmp);
传入的参数是 s,可以理解为 str *,即 指向字符串的指针
使用 sort 需要处理几个问题:
1. ++操作;
2. 比较;
3. 赋值
你对于第三个没有处理,
假定有变量
str a,b;
a = b; // 这样是不可以的
- 6 回答
- 0 关注
- 1671 浏览
添加回答
举报