如何使用可比较的 C# 比较泛型中的字符串和整数
How to compare Strings & ints in Generic Using comparable C#
我已经尝试使用 IComparable 并进行了研究,但我不明白为什么这不起作用。
我想比较字符串和整数,但我不明白如何使用 ICompareable 接口对通用类型进行比较。
如何实现 CompareTo 来处理泛型?
public class QuickSort : IComparable
{
public static int Partition<T>(ref T[] arr, int lo, int hi)
{
int i = lo + 1;
int j = hi;
while (j > i)
{
while (arr[i].) // <- Cannot apply CompareTo
i++;
while (arr[j] > arr[lo])
j--;
Swap(ref arr[i], ref arr[j]);
}
Swap(ref arr[lo], ref arr[j]);
return j;
}
}
您需要明确指定您的 T
实现 IComparable
。将您的方法声明更改为如下所示:
public static int Partition<T>(ref T[] arr, int lo, int hi) where T : IComparable
此外,您不能像 arr[i] > arr[j]
那样比较您的项目。您需要调用 CompareTo()
并检查它是否大于零、小于零或等于零:
while(arr[i].CompareTo(arr[high]) > 0)
(在您的情况下,这意味着 arr[i] > arr[high]
。)
而且您很可能不需要在 QuickSort
class 上实施 IComparable
。 IComparable
需要为那些将要比较的项目实施,而不是为比较器 class 本身实施。
您必须指定 T
可以进行比较,否则编译器无法知道期望的类型。这样你就可以对你的两个条件使用 CompareTo
:
public class QuickSort
{
public static int Partition<T>(T[] arr, int lo, int hi) where T : IComparable
{
int i = lo + 1;
int j = hi;
while (j > i)
{
while (arr[i].CompareTo(arr[lo]) == 0)
i++;
while (arr[j].CompareTo(arr[lo]) > 0) // means arr[j] > arr[lo]
j--;
Swap(ref arr[i], ref arr[j]);
}
Swap(ref arr[lo], ref arr[j]);
return j;
}
}
另外,我认为你不想比较不同的 QuickSort
个实例,所以我删除了它的界面。
更新:根据 Hawkmooon 的评论,我再次查看了您的方法,认为它的签名可以更简单:
public static int Partition(IComparable[] arr, int lo, int hi)
{
int i = lo + 1;
int j = hi;
while (j > i)
{
while (arr[i].CompareTo(arr[lo]) == 0)
i++;
while (arr[j].CompareTo(arr[lo]) > 0)
j--;
Swap(ref arr[i], ref arr[j]);
}
Swap(ref arr[lo], ref arr[j]);
return j;
}
我认为您的代码可能缺少某些内容。以防万一,here you have a full C# example of a working quicksort method.
我已经尝试使用 IComparable 并进行了研究,但我不明白为什么这不起作用。 我想比较字符串和整数,但我不明白如何使用 ICompareable 接口对通用类型进行比较。
如何实现 CompareTo 来处理泛型?
public class QuickSort : IComparable
{
public static int Partition<T>(ref T[] arr, int lo, int hi)
{
int i = lo + 1;
int j = hi;
while (j > i)
{
while (arr[i].) // <- Cannot apply CompareTo
i++;
while (arr[j] > arr[lo])
j--;
Swap(ref arr[i], ref arr[j]);
}
Swap(ref arr[lo], ref arr[j]);
return j;
}
}
您需要明确指定您的 T
实现 IComparable
。将您的方法声明更改为如下所示:
public static int Partition<T>(ref T[] arr, int lo, int hi) where T : IComparable
此外,您不能像 arr[i] > arr[j]
那样比较您的项目。您需要调用 CompareTo()
并检查它是否大于零、小于零或等于零:
while(arr[i].CompareTo(arr[high]) > 0)
(在您的情况下,这意味着 arr[i] > arr[high]
。)
而且您很可能不需要在 QuickSort
class 上实施 IComparable
。 IComparable
需要为那些将要比较的项目实施,而不是为比较器 class 本身实施。
您必须指定 T
可以进行比较,否则编译器无法知道期望的类型。这样你就可以对你的两个条件使用 CompareTo
:
public class QuickSort
{
public static int Partition<T>(T[] arr, int lo, int hi) where T : IComparable
{
int i = lo + 1;
int j = hi;
while (j > i)
{
while (arr[i].CompareTo(arr[lo]) == 0)
i++;
while (arr[j].CompareTo(arr[lo]) > 0) // means arr[j] > arr[lo]
j--;
Swap(ref arr[i], ref arr[j]);
}
Swap(ref arr[lo], ref arr[j]);
return j;
}
}
另外,我认为你不想比较不同的 QuickSort
个实例,所以我删除了它的界面。
更新:根据 Hawkmooon 的评论,我再次查看了您的方法,认为它的签名可以更简单:
public static int Partition(IComparable[] arr, int lo, int hi)
{
int i = lo + 1;
int j = hi;
while (j > i)
{
while (arr[i].CompareTo(arr[lo]) == 0)
i++;
while (arr[j].CompareTo(arr[lo]) > 0)
j--;
Swap(ref arr[i], ref arr[j]);
}
Swap(ref arr[lo], ref arr[j]);
return j;
}
我认为您的代码可能缺少某些内容。以防万一,here you have a full C# example of a working quicksort method.