按特定值对struct-members进行排序

时间:2014-10-18 08:37:27

标签: c++ c arrays sorting struct

我已经有这个问题了一段时间,所以我觉得最好问一下方向。在没有任何帮助的情况下尝试搜索并且现在已经很久了,所以我们走了。

我正在学习C ++并且很难完成这项练习:我必须使用struct保存3名学生的详细信息,然后按照他们离学校的距离按升序打印。第一个学生的详细信息由用户输入询问,其他人则由变量初始化。一切都很顺利,直到我必须解决它们。

目前我正在尝试将数组传递给排序函数,其中包含我手动输入到该数组的每个学生详细信息以进行测试。当我尝试直接从struct成员值初始化数组然后将该数组传递给排序func时,我遇到了一些严重的问题。此外,在本练习中,我必须使用C阵列。

Perpaps有人可以告诉我这样做的正确方法吗?也许有更简单的方法来做到这一点?提前致谢!这是我的代码:

编辑:平台:Windows 7,IDE:Visual Studio 2013

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

struct personaldata {
    char firstname[50], lastname[50], address[50], postal[50];
    double distschool;
    int shoesize;
};

struct SortRecordsAscending
{
    bool operator()(const personaldata& lhs, const personaldata& rhs)
    {
        return (lhs.distschool < rhs.distschool);
    }
};

void SortingObjectsExample(personaldata *details, int SIZE)
{
    sort(details, details + SIZE, SortRecordsAscending());
    for (int i = 0; i < SIZE; ++i)
    {
        cout << details[i].firstname << " " << details[i].lastname << endl;
        cout << details[i].address << " " << details[i].postal << endl;
        cout << "Distance from school: " << details[i].distschool << endl;
        cout << "Shoe size: " << details[i].shoesize << endl;
        cout << endl;
    }
}

int main() {

    personaldata student1, student2, student3;
    const int SIZE(3); // Amount of students


    //Student 1
    cout << "Enter first name: ";
    cin.getline(student1.firstname, 50);

    cout << "Enter last name: ";
    cin.getline(student1.lastname, 50);

    cout << "Enter distance from school: ";
    cin >> student1.distschool;
    cin.ignore();

    cout << "Enter address: ";
    cin.getline(student1.address, 50);

    cout << "Enter postal: ";
    cin >> student1.postal;

    cout << "Enter shoe size: ";
    cin >> student1.shoesize;

    //Student 2
    strcpy_s(student2.firstname, 50, "Olli");
    strcpy_s(student2.lastname, 50, "Oppilas");
    student2.distschool = 9.4;
    strcpy_s(student2.address, 50, "Opiskelijakatu 5a 14");
    strcpy_s(student2.postal, 50, "40200");
    student2.shoesize = 39;

    //Student 3
    strcpy_s(student3.firstname, 50, "Ossi");
    strcpy_s(student3.lastname, 50, "Oppilas");
    student3.distschool = 8.4;
    strcpy_s(student3.address, 50, "Koululaiskatu 18b 2");
    strcpy_s(student3.postal, 50, "40100");
    student3.shoesize = 41;

    personaldata details[3] = {
            { "Oiva", "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
            { "Ossi", "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
            { "Olli", "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
    };

    SortingObjectsExample(details, SIZE);

}

上面的代码按我想要的方式工作。但是,如果我喜欢这样:

personaldata details[3] = {
        { student1.firstname, "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
        { student2.firstname, "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
        { student3.firstname, "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
};

SortingObjectsExample(details, SIZE);

我从编译器

获得
1>------ Build started: Project: Harj17, Configuration: Debug Win32 ------
1>  harj17.cpp
1>harj17.cpp(80): error C2440: 'initializing' : cannot convert from 'char (*)[50]' to 'char'
1>There is no context in which this conversion is possible
1>harj17.cpp(80): warning C4244: 'initializing' : conversion from 'double' to 'char', possible loss of data
1>harj17.cpp(80): error C2440: 'initializing' : cannot convert from 'char [50]' to 'char'
1>          There is no context in which this conversion is possible
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果使用这样的指针:

    personaldata details[3] = {
        { *student1.firstname, "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
        { *student2.firstname, "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
        { *student3.firstname, "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
};

它会编译程序但是有警告信息并且它无法正常工作,它只打印第一个人的名字。

    1>------ Build started: Project: Harj17, Configuration: Debug Win32 ------
1>  harj17.cpp
1>harj17.cpp(80): warning C4244: 'initializing' : conversion from 'double' to 'char', possible loss of data
1>  Harj17.vcxproj -> Harj17.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

这是运行程序时的输出。您可以看到名称问题,其他信息也被搞砸了。

Enter first name: Example
Enter last name: Student
Enter distance from school: 13.37
Enter address: Example Address 6B 16
Enter postal: 1337
Enter shoe size: 43
EOppilas Koivukatu 8B 16
40100   +
Distance from school: 0
Shoe size: 0

OOppilas Koululaiskatu 18b 2
40100)
Distance from school: 0
Shoe size: 0

OOppilas Lehtitie 3B 15
401200&
Distance from school: 0
Shoe size: 0

1 个答案:

答案 0 :(得分:1)

很奇怪,你可以使用STL算法而不是数据结构。我从你的问题中不清楚你遇到了什么困难。对结构进行排序或构建它们?无论如何,这是一种创建和排序结构的简单方法。由于您的排序功能不需要任何状态,因此它不需要是一个仿函数。它可以只是一个正常的功能。

为了进一步改进,我将使用向量,向结构添加流运算符并使用构造函数。

#include <algorithm>
using namespace std;

struct henkilotiedot { // Person first name, last name, address and postal
    string etunimi, sukunimi, osoite, postinumero;
    double koulumatka; // Distance from school
    int kengannumero; // Shoe size
};

bool SortByDistance(const henkilotiedot& lhs, const henkilotiedot& rhs)
{
    return lhs.koulumatka < rhs.koulumatka;
}

int main()
{
    const int SIZE = 3;
    henkilotiedot data[] = { { "blah", "blah", "blah", "blah", 5.0, 10 }, { "blah", "blah", "blah", "blah", 1.0, 10 }, { "blah", "blah", "blah", "blah", 15.0, 10 } };
    sort(data, data + SIZE, SortByDistance);
}