Sorting algorithm works for C-array but not works for std::span.

huangapple go评论64阅读模式
英文:

Sorting algorithm works for C-array but not works for std::span

问题

这个问题发生在我正在编写插入排序算法时。我尝试实现一个适用于C++20的std::span版本,但它抛出了"std::span索引超出范围"的错误。所以我将它改成了"C数组",相同的代码可以正常工作。我不知道问题出在哪里。P.S我的平台是Windows 10,我正在使用Visual Studio 2022。

这是std::span版本。

template <typename Ty>
void insertion_sort(std::span<Ty> arr) {
    long long size = arr.size();
    // 从第二个元素到第n个元素
    for (long long i = 1; i < size; ++i) {
        Ty key = arr[i];
        long long j = i - 1;
        // 直到第一个元素。
        for (; arr[j] > key && j >= 0; --j)
            arr[j + 1] = arr[j];
        // 将key放入正确的位置。
        arr[j + 1] = key;
    }
}

然而,如果我将std::span更改为C数组,如下所示,它可以正常工作。

template <typename Ty>
void insertion_sort(Ty* arr, std::size_t size) {
    // 从第二个元素到第n个元素
    for (long long i = 1; i < size; ++i) {
        Ty key = arr[i];
        long long j = i - 1;
        // 直到第一个元素。
        for (; arr[j] > key && j >= 0; --j)
            arr[j + 1] = arr[j];
        // 将key放入正确的位置。
        arr[j + 1] = key;
    }
}

如果有人能帮助我解决这个问题,我将不胜感激!

英文:

This problem happens when I am working on a insertion_sort algorithm.
I tried to implement a version for C++20's std::span but it throws a "std::span index out of range" So I turned it to "C-array" the same code works fine.
I don't know where is the problem. P.S My platform is windows10 and I'm using VisualStudio2022

This is the std::span version.

template &lt;typename Ty&gt;
void insertion_sort(std::span&lt;Ty&gt; arr) {
    long long size = arr.size();
    // From the 2nd element to the nth element
    for (long long i = 1; i &lt; size; ++i) {
        Ty key = arr[i];
        long long j = i - 1;
        // Until the first element.
        for (; arr[j] &gt; key &amp;&amp; j &gt;= 0; --j)
            arr[j + 1] = arr[j];
        // Place key into the right position.
        arr[j + 1] = key;
    }
}

However if I change std::span to C-array, like below, it works fine.

template &lt;typename Ty&gt;
void insertion_sort(Ty* arr, std::size_t size) {
    // From the 2nd element to the nth element
    for (long long i = 1; i &lt; size; ++i) {
        Ty key = arr[i];
        long long j = i - 1;
        // Until the first element.
        for (; arr[j] &gt; key &amp;&amp; j &gt;= 0; --j)
            arr[j + 1] = arr[j];
        // Place key into the right position.
        arr[j + 1] = key;
    }
}

I'll be glad if someone can help me fix this problem!

答案1

得分: 2

以下是翻译好的部分:

在for循环中的条件

for (; arr[j] &gt; key &amp;&amp; j &gt;= 0; --j)
    arr[j + 1] = arr[j];

是错误的。您需要交换逻辑AND运算符的操作数

for (; j &gt;= 0 &amp;&amp; arr[j] &gt; key; --j)
    arr[j + 1] = arr[j];

否则,表达式arr[j]的索引可以等于-1

英文:

The condition in the for loop

    for (; arr[j] &gt; key &amp;&amp; j &gt;= 0; --j)
        arr[j + 1] = arr[j];

is wrong. You need to exchange the operands of the logical AND operator

    for (; j &gt;= 0 &amp;&amp; arr[j] &gt; key; --j)
        arr[j + 1] = arr[j];

Otherwise the index of the expression arr[j] can be equal to -1.

huangapple
  • 本文由 发表于 2023年5月21日 16:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定