简体   繁体   English

函数中数组参数长度的默认参数值

[英]Default parameter value of array parameter length in a function

I made a function that quicksorts an array that is passed through the to the function but due to the function being a recursive one it takes 2 extra parameters that aid with the quick sort.我创建了一个函数,该函数对传递给函数的数组进行快速排序,但由于该函数是递归函数,它需要 2 个额外的参数来帮助快速排序。

After each round of the function it sets left or right as the next area to sort and so on.在每一轮函数之后,它将左或右设置为下一个要排序的区域,依此类推。

And due to these extra parameters every time I manually call the function to do a quick sort I need to add 0 and the array length as the parameters.由于这些额外的参数,每次我手动调用函数进行快速排序时,我都需要添加 0 和数组长度作为参数。

Quicksort(arr, 0, arr.Length);

This seems like it can be avoided but simply adding a default value to the left parameter (0) but the right parameter would need to be the length of the array which in this case is called elements , it would be:这似乎可以避免,但只需向左侧参数 (0) 添加默认值,但右侧参数需要是数组的长度,在这种情况下称为elements ,它将是:

public static void QuickSort<T>(T[] elements, int left = 0, int right = elements.Length) where T : IComparable

but getting the length of the elements array is not possible.但无法获取元素数组的长度。

I thought about making a function that would simply insert 0 and the length of the array instead of me but I wanted to find a way to do it without an extra function if it is possible.我考虑过创建一个函数来简单地插入 0 和数组的长度而不是我,但如果可能的话,我想找到一种方法来做到这一点而无需额外的函数。

public static void QuickSort<T>(T[] elements, int left, int right) where T : IComparable
{
    int i = left, j = right;
    T pivot = elements[(left + right) / 2];

    while (i <= j)
    {
        while (elements[i].CompareTo(pivot) < 0)
            i++;

        while (elements[j].CompareTo(pivot) > 0)
            j--;

        if (i <= j)
        {
            T tmp = elements[i];
            elements[i++] = elements[j];
            elements[j--] = tmp;
        }
    }
    if (left < j)
        QuickSort(elements, left, j);

    if (i < right)
        QuickSort(elements, i, right);
}

Sometimes the easiest solution is the best one: add an overload.有时最简单的解决方案是最好的解决方案:添加重载。

public static void QuickSort<T>(T[] elements, int left = 0) where T : IComparable
{
    QuickSort(elements, left, elements.Length);
}

public static void QuickSort<T>(T[] elements, int left, int right) where T : IComparable
{
    // code
}

Ugly solution (use a nullable type and change the null vale with the Length ).丑陋的解决方案(使用可为空的类型并使用Length更改null值)。 null isn't part of the legal domain of right , so no big problem: null不属于right的法律领域,所以没有什么大问题:

public static void QuickSort<T>(T[] elements, int left = 0, int rightTemp = null) where T : IComparable
{
    int right = rightTemp ?? elements.Length;

    // code
}

Uglier solution (use -1 as the "value-to-be-replaced").更丑的解决方案(使用-1作为“要替换的值”)。 -1 (or int.MinValue ) isn't part of the legal domain of right , but this solution is ugly as hell :-) -1 (或int.MinValue )不属于right的法律领域,但这个解决方案非常丑陋:-)

public static void QuickSort<T>(T[] elements, int left = 0, int right = -1) where T : IComparable
{
    if (right == -1)
    {
        right = elements.Length;
    }

    // code
}

You need a method with a nice, clean, minimal signature to expose to the outside world and a private recursive method that will do the sorting.您需要一个具有漂亮、干净、最小签名的方法来向外界公开,以及一个私有递归方法来进行排序。

So:所以:

// this method is exposed to the outside world.
// Nobody needs to know about "left" and "right".
// Consumers of this method just want to "QuickSort" some elements. 
public static void QuickSort<T>(T[] elements) where T : IComparable
{
    // init the recursion here and forget about it
    QuickSortInternal(elements, 0 , elements.Length);
}

// this is your recursive method
private static void QuickSortInternal<T>(T[] elements, int left, int right) where T : IComparable
{
    // your code....
}

Since you are always sending through the array itself as the first parameter, you have the length anyway.由于您始终将数组本身作为第一个参数发送,因此无论如何您都有长度。 Why send through the length at all, if you can just get the length of the array from the first parameter?如果您可以从第一个参数中获取数组的长度,为什么还要发送长度?

In other words, change the code a bit, so that each call to Quicksort sends through an array only.换句话说,稍微更改一下代码,以便对 Quicksort 的每次调用仅通过一个数组发送。 Dont send left and right at all.根本不发送左右。 Split the array you get in in each call, and then send the split array to the two sub Quicksort calls, without sending left and right each time.拆分你在每次调用中得到的数组,然后将拆分后的数组发送给两个子 Quicksort 调用,而不是每次都发送左右。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM