我已經有一段時間了現在這個問題,所以我認爲這將是更好的要求方向。嘗試搜索,沒有任何幫助,現在正在努力爭取這麼長時間,所以我們走了。按特定值排序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
只是一點點的建議。翻譯你的變量和類成員的名字。它會真正幫助我們跟蹤正在執行的操作:) – Rerito 2014-10-18 08:43:16
*「當我嘗試從結構成員值直接初始化數組,然後將該數組傳遞給sort func時,我遇到了一些嚴重問題。」* ...以及哪些問題那些?您需要指定是否存在運行時錯誤,編譯時錯誤; *和*命名確切的故障和線路。另外,你可能不知道在這裏問幾個有重點的問題比一個大問題更好。如果你的問題是關於輸入的,那麼詢問輸入;如果是關於排序問題,並且可以從這個問題中移除輸入,那麼效果會更好。 (也請注意'strcpy_s'是微軟專用的,而不是gcc。) – HostileFork 2014-10-18 09:09:42
@Rerito好的,我翻譯了這些變量,抱歉,在發佈我的問題時很匆忙。:) – mpak 2014-10-18 13:16:33