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

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

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版本。

  1. template <typename Ty>
  2. void insertion_sort(std::span<Ty> arr) {
  3. long long size = arr.size();
  4. // 从第二个元素到第n个元素
  5. for (long long i = 1; i < size; ++i) {
  6. Ty key = arr[i];
  7. long long j = i - 1;
  8. // 直到第一个元素。
  9. for (; arr[j] > key && j >= 0; --j)
  10. arr[j + 1] = arr[j];
  11. // 将key放入正确的位置。
  12. arr[j + 1] = key;
  13. }
  14. }

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

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

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

英文:

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.

  1. template &lt;typename Ty&gt;
  2. void insertion_sort(std::span&lt;Ty&gt; arr) {
  3. long long size = arr.size();
  4. // From the 2nd element to the nth element
  5. for (long long i = 1; i &lt; size; ++i) {
  6. Ty key = arr[i];
  7. long long j = i - 1;
  8. // Until the first element.
  9. for (; arr[j] &gt; key &amp;&amp; j &gt;= 0; --j)
  10. arr[j + 1] = arr[j];
  11. // Place key into the right position.
  12. arr[j + 1] = key;
  13. }
  14. }

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

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

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

答案1

得分: 2

以下是翻译好的部分:

在for循环中的条件

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

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

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

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

英文:

The condition in the for loop

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

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

  1. for (; j &gt;= 0 &amp;&amp; arr[j] &gt; key; --j)
  2. 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:

确定