Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换C++;程序到CSharp程序。C++;在CSharp中出现异常时运行 < >我把C++程序转换成cSpice程序。C++和CpHARK完全相同,C++程序在输入{ 5, 4, 1,3, 6, 7,2 }时没有问题,而在CpHARP中,索引有界外异常。我已经粘贴了下面的代码,它们完全是彼此的副本,但是我不能理解为什么在CpHARP版本中有异常,而C++运行良好。p>_C#_C++ - Fatal编程技术网

C# 转换C++;程序到CSharp程序。C++;在CSharp中出现异常时运行 < >我把C++程序转换成cSpice程序。C++和CpHARK完全相同,C++程序在输入{ 5, 4, 1,3, 6, 7,2 }时没有问题,而在CpHARP中,索引有界外异常。我已经粘贴了下面的代码,它们完全是彼此的副本,但是我不能理解为什么在CpHARP版本中有异常,而C++运行良好。p>

C# 转换C++;程序到CSharp程序。C++;在CSharp中出现异常时运行 < >我把C++程序转换成cSpice程序。C++和CpHARK完全相同,C++程序在输入{ 5, 4, 1,3, 6, 7,2 }时没有问题,而在CpHARP中,索引有界外异常。我已经粘贴了下面的代码,它们完全是彼此的副本,但是我不能理解为什么在CpHARP版本中有异常,而C++运行良好。p>,c#,c++,C#,C++,C++程序 #include <iostream> using namespace std; #include <stdio.h> #include<stdlib.h> void swap (int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int partition (int A[], int l, int h) { int pivot = A[l]; int i = l,

C++程序

#include <iostream>
using namespace std;

#include <stdio.h>
#include<stdlib.h>

void swap (int *x, int *y)
{
  int temp = *x;
  *x = *y;
  *y = temp;
}

int partition (int A[], int l, int h)
{
  int pivot = A[l];
  int i = l, j = h;

  do
    {
      do
    {
      i++;
    } while (A[i] <= pivot);
  do
    {
      j--;
    } while (A[j] > pivot);

  if (i < j)
    swap (&A[i], &A[j]);
}
while (i < j);

  swap (&A[l], &A[j]);
  return j;
    }

void QuickSort (int A[], int l, int h)
{
  int j;

  if (l < h)
    {
      j = partition (A, l, h);
      QuickSort (A, l, j);
      QuickSort (A, j + 1, h);
    }
}

int main ()
{
  int A[] = { 5, 4, 1, 3, 6, 7, 2 }, n = 7, i;

  QuickSort (A, 0, n);

  for (i = 0; i < 7; i++)
    printf ("%d ", A[i]);
  printf ("\n");

  return 0;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApp
{
    public class QuickSort2
    {
        int partition(int[] A, int l, int h)
        {
            int pivot = A[l];
            int i = l, j = h;

            do
            {
                do
                {
                    i++;
                }
                while (A[i] <= pivot);
                do
                {
                    j--;
                }
                while (A[j] > pivot);

                if (i < j)
                    Swap(ref A[i], ref A[j]);
            }
            while (i < j);

            Swap(ref A[l], ref A[j]);
            return j;
        }

        public void QuickSort(int[] A, int l, int h)
        {
            int j;

            if (l < h)
            {
                j = partition(A, l, h);
                QuickSort(A, l, j);
                QuickSort(A, j + 1, h);
            }
        }

        public void Swap(ref int x, ref int y)
        {

            int tmp = x;
            x = y;
            y = tmp;
        }
    }
}

[Test]
public void TestQuickSort2()
{
    QuickSort2 quick = new QuickSort2();
    int[] list = new int[] { 5, 4, 1, 3, 6, 7, 2};
    quick.QuickSort(list, 0, 7);
    for (int i = 0; i < 7; i++)
    {
        Console.Write(list[i] + " --> ");
    }

}
#包括
使用名称空间std;
#包括
#包括
无效交换(int*x,int*y)
{
int temp=*x;
*x=*y;
*y=温度;
}
整数分区(整数A[],整数l,整数h)
{
int pivot=A[l];
int i=l,j=h;
做
{
做
{
i++;
}while(一个[i]支点);
if(i
CSharp程序

#include <iostream>
using namespace std;

#include <stdio.h>
#include<stdlib.h>

void swap (int *x, int *y)
{
  int temp = *x;
  *x = *y;
  *y = temp;
}

int partition (int A[], int l, int h)
{
  int pivot = A[l];
  int i = l, j = h;

  do
    {
      do
    {
      i++;
    } while (A[i] <= pivot);
  do
    {
      j--;
    } while (A[j] > pivot);

  if (i < j)
    swap (&A[i], &A[j]);
}
while (i < j);

  swap (&A[l], &A[j]);
  return j;
    }

void QuickSort (int A[], int l, int h)
{
  int j;

  if (l < h)
    {
      j = partition (A, l, h);
      QuickSort (A, l, j);
      QuickSort (A, j + 1, h);
    }
}

int main ()
{
  int A[] = { 5, 4, 1, 3, 6, 7, 2 }, n = 7, i;

  QuickSort (A, 0, n);

  for (i = 0; i < 7; i++)
    printf ("%d ", A[i]);
  printf ("\n");

  return 0;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApp
{
    public class QuickSort2
    {
        int partition(int[] A, int l, int h)
        {
            int pivot = A[l];
            int i = l, j = h;

            do
            {
                do
                {
                    i++;
                }
                while (A[i] <= pivot);
                do
                {
                    j--;
                }
                while (A[j] > pivot);

                if (i < j)
                    Swap(ref A[i], ref A[j]);
            }
            while (i < j);

            Swap(ref A[l], ref A[j]);
            return j;
        }

        public void QuickSort(int[] A, int l, int h)
        {
            int j;

            if (l < h)
            {
                j = partition(A, l, h);
                QuickSort(A, l, j);
                QuickSort(A, j + 1, h);
            }
        }

        public void Swap(ref int x, ref int y)
        {

            int tmp = x;
            x = y;
            y = tmp;
        }
    }
}

[Test]
public void TestQuickSort2()
{
    QuickSort2 quick = new QuickSort2();
    int[] list = new int[] { 5, 4, 1, 3, 6, 7, 2};
    quick.QuickSort(list, 0, 7);
    for (int i = 0; i < 7; i++)
    {
        Console.Write(list[i] + " --> ");
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间MyApp
{
公共类快速入门2
{
int分区(int[]A,int l,int h)
{
int pivot=A[l];
int i=l,j=h;
做
{
做
{
i++;
}
while(一个[i]支点);
if(i”);
}
}
编辑2。 问题发生在以下位置和迭代


<>这里的问题是C++没有检查边界,而C是按照@ KaNbBuin提出的。因此,添加以下绑定检查解决了该问题

    int partition(int[] A, int l, int h)
    {
        int pivot = A[l];
        int i = l, j = h;

        do
        {
            do
            {
                i++;
            }
            while (i < h && A[i] <= pivot);  // i < h condition not needed in C++ as C++ doesnot check for array bounds but C# does
            do
            {
                j--;
            }
            while (j >= l && A[j] > pivot);

            if (i < j)
                Swap(ref A[i], ref A[j]);
        }
        while (i < j);

        Swap(ref A[l], ref A[j]);
        return j;
    }
int分区(int[]A,int l,int h)
{
int pivot=A[l];
int i=l,j=h;
做
{
做
{
i++;
}
而(ipivot);
if(i
正如我最初要求的那样,问题就在那里。本声明:

do {
  i++;
} while (A[i] <= pivot);
do{
i++;

}while(A[i]你能放一些try/catch子句来找出错误发生的地方和在哪个迭代中吗?我怀疑在某个时候你试图访问一个[7],这超出了范围。在你的C#程序(没有main,所以我们不知道)中,你试过快速排序(A,0,6)吗?请提供C#程序的
Main
方法。此外,数组从索引0开始,在n-1结束(
n
是数组长度)。
n=7
在程序中,以及在编写
while时(A[j]>pivot)<代码> >因为在开始时<代码> j=7 < /COD> C++不检查数组绑定,但C ^确实。您应该做<代码> Quask排序(A,0, 6)< /C>。而C++和CpHARP完全相同——当(Ai)时,什么??@ USE109260<代码>边注:C++是一个具有非常强大的标准库的语言。不需要编写自己的代码>交换< /COD>。也不需要编写自己的排序算法,但我有一种感觉,它是一个赋值。无论如何,用代码空间使用代码空间写一个<代码>交换< /代码>函数,使用名称空间STD;< /Cord>让你进入K的乐趣世界。如果你的<代码>交换<代码>或库的<代码>::交换SW/COD>将被调用。不,即使C++程序不检查数组绑定,也不意味着你应该访问数组绑定的元素,因为这可能导致不可预测的结果(即使在程序中看起来安全)。使用<代码> Quask排序(A,0, 6)因为你的函数包含范围参数(包括上界6)@ KaNbYuin——不,整个算法使用一个排他上限。参见<代码> Quask排序(a,l,j);< /C> >,当然,C++中的BooScEcChink当然也是正确的。