-6
我刚刚完成作业,但最后一节正在杀死我。我已经完成了研究,解决方案是我们在课堂上没有教过的概念,并且完全凌驾于我的头上。我需要bubbleSort和结构数组。但是我完全失去了。任何帮助将不胜感激。气泡排序结构数组
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
struct billingInfo;
void bubbleSort(billingInfo list[], int length);
using namespace std;
int main()
{
// create struct with a field for zip codes and name of recepients that are being imported from text document
struct billingInfo {
int zipCode;
string name;
};
// create array of billingInfo struct
billingInfo recipients[20];
// open text file and test if file opened
ifstream dataFile("NameZip.txt");
if (dataFile.fail())
cout << "Can't open file." << endl;
else cout << "File opened." << endl;
int numElements = 0;
while (dataFile.peek() != EOF) {
dataFile >> recipients[numElements].zipCode;
getline(dataFile, recipients[numElements].name);
cout << recipients[numElements].zipCode << endl;
cout << recipients[numElements].name << endl;
numElements++;
}
system("pause");
return 0;
}
void bubbleSort(billingInfo list[], int length) {
billingInfo temp;
int itterator;
int index;
for (itterator = 1; itterator < length - itterator - length; itterator++) {
for (index = 0; index < length - itterator; index++) {
temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
enter code here
}
究竟什么是您所遇到的问题?此外,这不是如何拼写迭代器 – xaxxon
“_Bubble对结构数组进行排序” - “struct”是引用数据类型,因此此类型的变量(对象)没有可用作参数的原始可比较值进行分类。你将不得不根据struct的成员变量的值来评估'struct'对象。在你的代码中,唯一的数字值由'int zipCode'保存。你究竟如何排序?是否应该按照邮政编码的值升序/降序排列? – progyammer
对不起,太模糊了。我将按照字母顺序排列字段名称,排序为 –