gpt4 book ai didi

c++ - 冒泡排序给出访问冲突

转载 作者:行者123 更新时间:2023-11-28 07:53:57 26 4
gpt4 key购买 nike

大家好,我正在尝试完成类作业,我需要根据员工的 ID 号对充满员工的文件进行排序。文件中有 10 行,每行都有一个员工信息。顺序是 ID LASTNAME FIRSTNAME

在我编写排序函数并将所有数据正确复制到数组之前,程序运行良好,但现在在添加我的排序函数后,我不断遇到访问冲突,但没有提示是什么原因造成的。

如有任何帮助,我将不胜感激。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Employee
{
public:
int _id;
string _lastName;
string _firstName;

Employee()
{
_id = 0;
_lastName = "n/a";
_firstName = "n/a";
}
};

void copyFile10(Employee [], int);
void sortFile10(Employee [], int);
int main()
{
const int size10 = 10;
Employee employees10[size10];

copyFile10(employees10, size10); //1.fill array/copy file
sortFile10(employees10, size10); //2. sort

system("pause");
return 0;
}

void copyFile10(Employee employees10[], const int size)
{
ifstream data10("data_10.dat");
for(int count = 0; count < 10; count++) //1.fill array/copy file
{
data10 >> employees10[count]._id;
data10 >> employees10[count]._lastName;
data10 >> employees10[count]._firstName;
}
data10.close();
}

void sortFile10(Employee employees10[], const int size)
{
Employee buff1;
Employee buff2;
int counter = 0;
bool ordered = false;

while (ordered == false)
{
for(int count = 0; count < size-1; count++)
{
if(employees10[count]._id > employees10[count+1]._id)
{
buff1._id = employees10[count+1]._id;
buff1._lastName = employees10[count+1]._lastName;
buff1._firstName = employees10[count+1]._firstName;

buff2._id = employees10[count]._id;
buff2._lastName = employees10[count]._lastName;
buff2._firstName = employees10[count]._firstName;

employees10[count]._id = buff1._id;
employees10[count]._lastName = buff1._lastName;
employees10[count]._firstName = buff1._firstName;

employees10[count+1]._id = buff2._id;
employees10[count+1]._lastName = buff2._lastName;
employees10[count+1]._lastName = buff2._lastName;

counter++;
}
if(counter == 0)
ordered = true;
else
counter = 0;
}
}
}

最佳答案

for(int count = 0; count < size; count++)
{
if(employees10[count]._id > employees10[count+1]._id)

在循环的最后一次迭代中发生了什么(即当 count 为 9 时)?

关于c++ - 冒泡排序给出访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com