[英]c++ sort with structs
我很難解決這個問題,需要一種客戶名稱,客戶ID,最后是應付金額。 我有整個程序,但無法弄清楚進行排序所需的最后一個原型。 我有一個名為Customers的結構,我也將提供int main()部分。 我只需要任何幫助來啟動原型SortData()。
struct Customers {
string Name;
string Id;
float OrderAmount;
float Tax;
float AmountDue;
};
const int MAX_CUSTOMERS = 1000;
bool MoreCustomers(int);
Customers GetCustomerData();
void OutputResults(Customers [], int);
void SortData(const int, const int, Customers []);
int main() {
Customers c[MAX_CUSTOMERS];
int Count = 0;
do {
c[Count++] = GetCustomerData();
} while (MoreCustomers(Count));
for (int i = 0; i < Count; i++) {
c[i].Tax = 0.05f * c[i].OrderAmount;
c[i].AmountDue = c[i].OrderAmount + c[i].Tax;
}
SortData(0, Count, c); //0:Sorts by customer name
OutputResults(c, Count);
GeneralSort(1, Count, c); //1:Sorts by ID
OutputResults(c, Count);
GeneralSort(2, Count, c); //2: Sorts by amount due
OutputResults(c, Count);
return 0;
}
void SortData(const int SortItem, const int count, CustomerProfile c[]) {
//0: Sort by name
//1: Sort by ID
//3: Sort by amount due
}
您應該使用C ++的標准排序函數std::sort
,在<algorithm>
標頭中聲明。
使用自定義排序功能排序時,必須提供謂詞函數 ,該函數指示左側值是否小於右側值。 因此,如果您希望先按名稱排序,然后按ID排序,然后按應付金額,按升序排序,您可以執行以下操作:
bool customer_sorter(Customer const& lhs, Customer const& rhs) {
if (lhs.Name != rhs.Name)
return lhs.Name < rhs.Name;
if (lhs.Id != rhs.Id)
return lhs.Id < rhs.Id;
return lhs.AmountDue < rhs.AmountDue;
}
現在,將該函數傳遞給您的sort
調用:
std::sort(customers.begin(), customers.end(), &customer_sorter);
這里假設你有一個STL容器(而不是一個數組,就像你在你的示例代碼中有)稱為customers
包含客戶。
經常忽略的是,您可以將STL范圍函數與基於C的數組一起使用,就像在您的示例中一樣。 所以你實際上不必轉向使用基於STL的容器(我不會在這里討論這樣做的優點:-))。
因此,基於Chris的答案,您可以調用如下排序:
std::sort( customers, customers+Count, &customer_sorter);
您只需要編寫一個比較兩個CustomerProfile類型的比較函數。 完成此功能后,您可以使用STL排序(請參閱http://www.sgi.com/tech/stl/sort.html或http://msdn.microsoft.com/en-us/library/ecdecxh1 (VS.80).aspx )或舊的C qsort: http : //en.wikipedia.org/wiki/Qsort_( C_Standard_Library ) 。 我建議不要編寫自己的排序算法,除非這是一個家庭作業。 您的比較取決於您喜歡使用的技術,它可能看起來像這樣:
int CompareCustomerProfile(
const CustomerProfile* pC1,
const CustomerProfile* pC2)
{
int result = strcmp(pC1->name, pC2->name);
if (0 != result) return result;
result = strcmp(pC1->ID, pC2->ID);
if (0 != result) return result;
if (pC1->amountDue < pC2->amountDue) return -1;
if (pC1->amountDue > pC2->amountDue) return 1;
return 0
}
這假設您的示例中的'string'類型是char *。 如果使用Unicode或多字節類型,則顯然必須使用適當的Unicode或多字節比較。 然后你只需用比較函數調用算法。 例如。 使用qsort:
qsort(c, Count, sizeof(CustomerProfile), CompareCustomerProfiler).
現在,如果這是一個家庭作業,你不應該問這里怎么做...
您可以使用創意Google搜索在C ++中找到許多排序實現。唯一的區別是,不是排序數字,而是排序結構。
因此,無論你在算法中使用if(a[i]<a[j])
,都會調用`if(isFirstCustomerLowerThanOther(a [i])
現在,使用以下結構創建一個函數:
bool isFirstCustuomerLowerThanOther(const Customer& firstCustomer, const Customer& secondCustomer)
{
// Implement based on your key preferences
}
更好的是,如果你使用C ++,你可以使用STL的排序algortihm(再次,google獲取信息以及如何將訂單傳遞給它。
我假設你是編程或C ++的新手,所以這里是你可能正在尋找的東西:
#include <search.h> // for the qsort()
int
CompareByName( const void *elem1, const void *elem2 )
{
return ((Customers*)elem1)->Name > ((Customers*)elem2)->Name? 1 : -1;
}
int
CompareByOrderAmount( const void *elem1, const void *elem2 )
{
return ((Customers*)elem1)->OrderAmount > ((Customers*)elem2)->OrderAmount? 1 : -1;
}
void SortData( int SortItem, int count, Customers customers[] )
{
switch (SortItem) {
case 0:
qsort(customers, count, sizeof(Customers), CompareByName);
break;
case 1:
qsort(customers, count, sizeof(Customers), CompareByOrderAmount);
break;
// ...
}
}
void test()
{
Customers cust[10];
cust[0].Name = "ten";
cust[1].Name = "six";
cust[2].Name = "five";
SortData( 0, 3, cust );
cout << cust[0].Name << endl;
cout << cust[1].Name << endl;
cout << cust[2].Name << endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.