原code使用的诠释阵列。需要更改为矢量数组数组、阵列、矢量、code

2023-09-12 21:24:41 作者:Acolasia(放纵)

TL; DR:帮我传递载体阵列输入功能,原本参加了廉政指针和整数

TL;DR: Help me pass vector array input into a function that originally took in int pointers and ints.

我是被了解的知名算法大专班。在这个类中,我们使用C ++为$ C C给定的算法$。有没有pre-REQ班学习C ++的,所以我的知识是pretty的低,当涉及到一些编程的主要的东西。

I am in a college class that is learning about well-known algorithms. In this class, we use C++ to code the given algorithms. There was no pre-req class to learn C++, so my knowledge is pretty low when it comes to some of the major stuff with programming.

我的问题:我要创建一个程序,它需要一个输入文件,它与用户的选择排序算法进行排序,并将结果写入到输出文件。我原来的code,它完美的作品使用了20个项目的输入文件,放入长度为20的数组,并分类与每一个人的排序算法没有问题的。

My problem: I have to create a program that takes an input file, sorts it with the user's choice of sorting algorithm, and write the results to an output file. My original code that works perfectly uses an input file of 20 items, placed into an array of length 20, and sorts no problem with each individual sorting algorithm.

从昨晚开始,我已经改变的唯一的事情是,我的输入信号通过一个向量数组,因为老师会给我们带来不同长度(10个项目到1,000,000项)的文件。我需要这些给定的输入文件进行排序4排序算法。其中只有一件作品,它不传递任何变量到函数。

Since last night, the only thing I have changed is that my input goes to a vector array, since the teacher will give us files of varying length (10 items to 1,000,000 items). I have four sorting algorithms that need to sort these given input files. Only one of them works, and it does not pass any variables into the function.

另外3最初传递数组的指针和其他变量,但是它们不与我的新的输入现在要一个向量数组,而不是一个int数组工作。我知道,我所传递需要改变,但我不知道如何正确地做到这一点。我试图从互联网(用指针和引用)上发现的来源很多不同的方式,但我有没有运气。这里是code,我使用了一些snipets。

The other 3 originally passed in array pointers and other variables, however they do not work with my new input now going to a vector array instead of an int array. I know that what I am passing in needs to be changed, but I have no idea how to do this correctly. I have tried many different ways from sources found on the internet (with pointers and references), but I have had no luck. Here is some snipets of the code I'm using.

vector<int> A;

void insertionSort() // This works no problem as is.

void split(int* A, int* B, int low, int high) //code for Merge-Sort
{
    //recurisvely calls split() and splitMerge()
}

void splitMerge(int* A, int* B, int low, int mid, int high) // more code for Merge-Sort
{
    // more code for Merge-Sort
}

//quickSort() and countSort() also pass ints and do not work either.

//small part of main()
for (i = 0; unsorted >> temp; i++)
    {
        A.push_back(temp);
        cout << A[i] << "\n";
        length++; //I use int length extensively in for-loops in my functions
    }

最后一件事。试图运行合并,排序,当我没有得到一个错误。它只是冻结我的控制台窗口。

Last thing. I do not get an error when trying to run the Merge-Sort. It just freezes my console window.

推荐答案

我认为没有足够的code键使在那里的错误可能是准确的prediction,但我认为问题是,排序算法做类似A ++与你的指针进入下一个成员。

I believe there is not enough code to make an accurate prediction on where the error may be, but I think the problem is that the sorting algorithms is doing something like A++ with you pointers to access the next member.

由于数组指针存储在里面的对象,而不是对存储相邻单元格的下一个数组,你的算法是骑自行车通过的东西,他们不应该。

Because arrays store the pointer to the next array inside the object and not on adjacent cells of memory, your algorithms are cycling through stuff they shouldn't.

如果是这种情况,该问题的解决方案是使用一个指针的一个迭代代替

If this is the case, the solution to your problem is to use an iterator instead of a pointer.

void split(A.begin(), B.begin(), int low, int high)