C++ 对结构数组进行冒泡排序

C++ 对结构数组进行冒泡排序,c++,bubble-sort,C++,Bubble Sort,我刚做完一项作业,但最后这一部分真让我受不了。我做过研究,解决方案是课堂上没有教过的概念,完全超出了我的理解范围。我需要bubbleSort和结构数组。但是我完全迷路了。任何帮助都将不胜感激 #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> struct billingInfo; void bubble

我刚做完一项作业,但最后这一部分真让我受不了。我做过研究,解决方案是课堂上没有教过的概念,完全超出了我的理解范围。我需要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

}

您到底遇到了什么问题?此外,这不是如何拼写iteratorBubble排序一个structs数组-struct是一个引用数据类型,因此这种类型对象的变量没有可以用作排序参数的原始可比较值。必须根据结构的成员变量的值计算结构对象。在代码中,唯一的数值由int-zipCode保存。你到底是怎么被要求分类的?它应该是按邮政编码值的升序/降序排列的吗?很抱歉这么模糊。我将使用气泡排序符按字母顺序对字段名进行排序。如果您发现答案正确,请接受并向上投票。
struct billingInfo
{
    int key; // Can be any key

};
typedef struct billingInfo billingInfo;

// Overload the > operator
bool operator> (const billingInfo& first,const billingInfo& second)
{
    return first.key > second.key;
}

// Make a custom Swap function for your struct
void swap (billingInfo& first,billingInfo& second)
{
    int temp;
    temp = first.key;
    first.key = second.key;
    second.key = temp;
}

// Not a single line of change in Bubble Sort Code.
// I hope the below code is bubble sort :p
void bubble_sort (billingInfo *list,int length) 
{
    for (int i=0;i<length-1;i++)
        for (int j=0;j<length-i-1;j++)
            if (list[j] > list[j+1]) // will work since we overloaded
                swap (list[j],list[j+1]); // Our custom function
}