2014-10-18 52 views
2

我已经有一段时间了现在这个问题,所以我认为这将是更好的要求方向。尝试搜索,没有任何帮助,现在正在努力争取这么长时间,所以我们走了。按特定值排序struct-members

我正在学习C++,并且在这个练习中遇到了很多困难:我必须使用struct保存3位学生的详细信息,然后按照他们与学校的距离以升序排列。第一名学生的详细信息由用户输入提问,其他人则通过变量进行初始化。一切都很好,直到我必须把它们整理出来。

此刻,我试图将数组传递给排序函数,其中包含每个学生的详细信息,我手动输入该数组以供测试。当我试图直接从struct成员值初始化数组,然后将该数组传递给sort 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 
+3

只是一点点的建议。翻译你的变量和类成员的名字。它会真正帮助我们跟踪正在执行的操作:) – Rerito 2014-10-18 08:43:16

+0

*“当我尝试从结构成员值直接初始化数组,然后将该数组传递给sort func时,我遇到了一些严重问题。”* ...以及哪些问题那些?您需要指定是否存在运行时错误,编译时错误; *和*命名确切的故障和线路。另外,你可能不知道在这里问几个有重点的问题比一个大问题更好。如果你的问题是关于输入的,那么询问输入;如果是关于排序问题,并且可以从这个问题中移除输入,那么效果会更好。 (也请注意'strcpy_s'是微软专用的,而不是gcc。) – HostileFork 2014-10-18 09:09:42

+0

@Rerito好的,我翻译了这些变量,抱歉,在发布我的问题时很匆忙。:) – mpak 2014-10-18 13:16:33

回答

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); 
} 
+0

好吧。我在这个网站上看到很多奇怪的C++教学方法,并且通过大学朋友的轶事证据。看起来很多教授认为以特定的方式做到“正确”。哦,我的,他们怎么会更加错误。 – 2014-10-18 13:12:04

+0

这种方式正是我想要的方式!唯一的问题是,这是用字符串完成的,因为我应该使用C数组(char)来完成此操作。当然,使用字符串会更好,但由于这是作业,我必须使用它们。我敢肯定,我非常接近它的工作,但是我不明白C阵列中有什么东西。 – mpak 2014-10-18 13:15:05

+0

@ Moo-Juice完全同意你的看法。我看过各种可怕的代码。 – 2014-10-18 13:16:00