C++ 使用未正确排序的向量进行合并排序
我正在研究Mergesort来刷新我对它的记忆。我是从我创建的人的文本文件中进行的。由于某种原因,我似乎无法调试。。。排序根本不是真正的排序。我有正确的函数和循环,但一定有一些小事情发生了,我没有注意到。我将感谢任何帮助。谢谢C++ 使用未正确排序的向量进行合并排序,c++,pointers,vector,mergesort,C++,Pointers,Vector,Mergesort,我正在研究Mergesort来刷新我对它的记忆。我是从我创建的人的文本文件中进行的。由于某种原因,我似乎无法调试。。。排序根本不是真正的排序。我有正确的函数和循环,但一定有一些小事情发生了,我没有注意到。我将感谢任何帮助。谢谢 #include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; struct Person
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
struct Person {
string DOB;
string balance;
string firstName;
string lastName;
string state;
Person() { }
Person(string DOB, string firstName, string lastName, string state, string balance) {
this->DOB = DOB;
this->firstName = firstName;
this->lastName = lastName;
this->state = state;
this->balance = balance;
}
void print() {
cout << DOB << " "
<< balance << " "
<< firstName<< " "
<< lastName << " "
<< state << " "
<< balance << "\n";
}
};
void print(vector<Person*> arr, int size) { // print function for array debuggin'
for (int i = 0; i < size - 1; i++) {
cout << arr[i]->lastName << " | ";
}
cout << endl;
}
void merge(vector<Person*> arr, int start, int mid, int end) {
int leftSize = mid - start + 1;
int rightSize = end - mid;
int leftIndex, rightIndex;
int masterIndex = start;
vector<Person*> tmpR; // arrays to represent our two sections of the total array
vector<Person*> tmpL;
for (leftIndex = 0; leftIndex < leftSize; leftIndex++) { // copying our values from our master array into our subarrays
tmpL.push_back(arr[leftIndex]);
}
for(rightIndex = 0; rightIndex < rightSize; rightIndex++) {
tmpR.push_back(arr[rightIndex]);
}
//print(tmpL, leftSize); // print those sub arrays out for debugging
//print(tmpR, rightSize);
leftIndex = 0;
rightIndex = 0;
while (leftIndex < leftSize && rightIndex < rightSize) { // compares L and R subarrays and picks the last name first in the alphabet to go first
if (tmpL[leftIndex]->lastName < tmpR[rightIndex]->lastName) {
arr[masterIndex] = tmpL[leftIndex];
leftIndex++;
} else {
arr[masterIndex] = tmpR[rightIndex];
rightIndex++;
}
masterIndex += 1;
}
while (leftIndex < leftSize) { // the two following while conditions empty the remaining ordered last names from the subArray that is not empty
arr[masterIndex] = tmpL[leftIndex];
leftIndex++;
masterIndex++;
}
while (rightIndex < rightSize) {
arr[masterIndex] = tmpR[rightIndex];
rightIndex++;
masterIndex++;
}
}
void split(vector<Person*> arr, int start, int end) {
if (start < end) {
int mid = (start+end) / 2;
split(arr, start, mid);
split(arr, mid + 1, end);
merge(arr, start, mid, end);
}
}
void readIn() {
string DOB;
string ssNumber;
string bankBalance;
string firstName;
string lastName;
string state;
int size;
vector<Person*> pVector;
ifstream fin;
fin.open("data1.txt");
if (fin.fail()) {
cout << ("error reading file");
exit(1);
}
while (!fin.eof()) { // pulls in our data and stores it in a poiinter to a person object
fin >> DOB >> ssNumber >> firstName >> lastName >> state >> bankBalance;
Person *tmp = new Person(DOB, firstName, lastName, state, bankBalance);
pVector.push_back(tmp);
}
size = (int) pVector.size() - 1;
fin.close(); // closes the input stream previously opened.
cout << size << endl;
sleep(2);
split(pVector, 0, size-1);
for (int i = 0; i < size; i++) {
cout << pVector[i]->lastName << endl;
}
}
int main() {
readIn();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构人{
字符串DOB;
弦天平;
字符串名;
字符串lastName;
字符串状态;
Person(){}
Person(字符串DOB、字符串firstName、字符串lastName、字符串状态、字符串余额){
这->DOB=DOB;
这个->名字=名字;
此->lastName=lastName;
这个->状态=状态;
这->平衡=平衡;
}
作废打印(){
cout据我所知,您正在通过值传递向量,也就是说,它会被复制,并且在调用站点上,您将保留原始向量
尝试通过引用传递向量
void split(vector<Person*>& arr, int start, int end) {
^^^
void merge(vector<Person*>& arr, int start, int mid, int end) {
^^^
void print(vector<Person*> const& arr, int size) {
^^^^^^^ // You don't want to modify it here
void拆分(向量和arr、整数开始、整数结束){
^^^
无效合并(矢量和arr、整数开始、整数中间、整数结束){
^^^
无效打印(矢量常量和arr,整数大小){
^^^^^^^//您不想在这里修改它
据我所知,您正在通过值传递向量,也就是说,它会被复制,并且在呼叫站点上,您将保留原始向量
尝试通过引用传递向量
void split(vector<Person*>& arr, int start, int end) {
^^^
void merge(vector<Person*>& arr, int start, int mid, int end) {
^^^
void print(vector<Person*> const& arr, int size) {
^^^^^^^ // You don't want to modify it here
void拆分(向量和arr、整数开始、整数结束){
^^^
无效合并(矢量和arr、整数开始、整数中间、整数结束){
^^^
无效打印(矢量常量和arr,整数大小){
^^^^^^^//您不想在这里修改它
我认为代码段中存在一个问题,即在循环中推送tmpL和tmpR向量中的值时,用0初始化leftIndex和rightIndex,而用start和rightIndex分别用mid+1初始化leftIndex和rightIndex,并分别运行循环到mid和end。我认为代码中存在一个问题其中,u在循环中推送tmpL和tmpR向量中的值,u用0初始化leftIndex和rightIndex,而u应该用start初始化leftIndex,用mid+1初始化rightIndex,并分别运行循环到mid和end。注意while(!fin.eof())
。您正在程序中使用一个强制转换。请记住强制转换的第一条规则:不要强制转换。在您的情况下,您只需要因为选择了错误的类型而进行强制转换。关于使用名称空间std;
,没有理由使用它。此外,避免使用原始指针,请查看std::unique\u ptr
。一旦它正常工作,请尝试一段时间查看。print
不打印最后一个数组元素。请简化为MVCE,此处的大多数代码与您的问题无关,请注意,while(!fin.eof())
。您正在程序中使用一个强制转换。请记住强制转换的第一条规则:不要强制转换。在您的情况下,您只需要因为选择了错误的类型而进行强制转换。关于使用名称空间std;
,没有理由使用它。此外,避免使用原始指针,请查看std::unique\u ptr
。一旦它正常工作,请尝试一段时间复习。<代码>打印< /COD>不打印最后一个数组元素。请将其缩小到MVCE,这里的大部分代码与您的问题无关,请参见Hy@ LokSistaRI,谢谢您的响应!C++中的数组是通过引用传递的,对吗?向量不会遵循同一规则吗?我尝试时,我运行程序时,第一个元素。t成为数组中的每一个元素。编辑:我错了,虽然数组是通过引用传递的,但假设向量是不正确的,这是不正确的。谢谢您的评论:我将相应地更新代码并运行测试。@BitShift:数组衰减为指针(不完全相同)你可以通过引用传递数组,但是语法是钝角的。嘿,洛基斯塔里,我感谢你的回应!C++中的数组是通过引用传递的,对吗?向量不会遵循同样的规则吗?我试着说,当我运行程序时,第一个元素变成了数组中的每一个元素。如果数组是通过引用传递的,则假设向量是不正确的,这是不正确的。谢谢您的评论:我将相应地更新代码并运行测试。@BitShift:数组会衰减为指针(不完全相同),这是C的遗留问题。您可以通过引用传递数组,但语法是迟钝的。