C++ c++;按结构排序

C++ c++;按结构排序,c++,arrays,sorting,struct,bubble-sort,C++,Arrays,Sorting,Struct,Bubble Sort,我很难处理这个问题,它需要一种客户名称、客户ID和最终到期金额。我已经计算出了整个程序,但无法计算出进行排序所需的最后一个原型。我有一个名为Customers的结构,我还将提供int main()部分。我只需要在原型SortData()上启动gt的任何帮助 struct客户{ 字符串名; 字符串Id; 浮动订单数量; 浮动税; 浮动应付金额; }; const int MAX_客户=1000; 布尔莫尔客户(国际); 客户获取CustomerData(); 无效输出结果(客户[],国际); 无效

我很难处理这个问题,它需要一种客户名称、客户ID和最终到期金额。我已经计算出了整个程序,但无法计算出进行排序所需的最后一个原型。我有一个名为Customers的结构,我还将提供int main()部分。我只需要在原型SortData()上启动gt的任何帮助

struct客户{
字符串名;
字符串Id;
浮动订单数量;
浮动税;
浮动应付金额;
};
const int MAX_客户=1000;
布尔莫尔客户(国际);
客户获取CustomerData();
无效输出结果(客户[],国际);
无效排序数据(常量int、常量int、客户[]);
int main(){
客户c[最大客户];
整数计数=0;
做{
c[Count++]=GetCustomerData();
}而(客户数);;
for(int i=0;i<代码> > p>你可以在C++中找到很多有创意的Google实现。
唯一的区别是,不是对数字进行排序,而是对结构进行排序


因此,只要有类似于
if(a[i]的东西,就应该使用C++的标准排序函数
std::sort
,在
标题中声明

使用自定义排序函数进行排序时,必须提供一个谓词函数,说明左侧值是否小于右侧值。因此,如果要先按名称排序,然后按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;
}

这假设您有一个名为
customers
的STL容器(而不是像示例代码中那样的数组)包含客户。

您只需要编写一个比较两种CustomerProfile类型的比较函数。一旦有了这个函数,您就可以使用STL排序(请参阅或)或者旧的C qsort:。我建议不要编写自己的排序算法,除非这是一个家庭作业。 您的比较取决于您喜欢使用的技术,它可能看起来像这样:

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
}

现在,如果这是一个家庭作业,你不应该在这里问如何做…

经常被忽略的是,你可以将STL范围函数与基于C的数组一起使用,就像在你的例子中一样。因此,你实际上不必转向使用基于STL的容器(我不会在这里讨论这样做的优点:-))

因此,根据Chris的回答,您可以调用sort,如下所示:

std::sort( customers, customers+Count, &customer_sorter);

<>我假设你是新的编程人员或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;
}
#包括//用于qsort()
int
CompareByName(常数void*elem1,常数void*elem2)
{
返回((客户*)elem1)->名称>((客户*)elem2)->名称?1:-1;
}
int
比较器比较器安装(常数无效*elem1,常数无效*elem2)
{
退货((客户*)elem1)->订单金额>((客户*)elem2)->订单金额?1:-1;
}
无效排序数据(整数排序、整数计数、客户[])
{
开关(SortItem){
案例0:
qsort(客户、计数、sizeof(客户)、CompareByName);
打破
案例1:
qsort(客户、计数、规模(客户)、CompareByOrderAmount);
打破
// ...
}
}
无效测试()
{
客户客户[10];
客户[0].Name=“十”;
客户[1].Name=“六”;
客户[2].Name=“五”;
SortData(0,3,cust);

您的答案显然是C风格的答案,没有C++的特性(除了使用过重载的字符串运算符)。如果这是一个C问题(并且您更改了字符串比较为STRCMP)你的答案是有用的,但它的方式,它根本没有问题。对于初学者,在C++风格下,排序应该使用STD::排序,不是Q排序。(即使在使用Q排序时,当两个元素相等时,你的比较函数必须处理这个情况,然后返回0。)是的,你是对的。但是使用plain struct也是C风格的。对于初学者来说,使用STL可能有点神秘。无论如何,我删除我的答案没有问题。我的比较函数应该处理元素相等的情况,对。但是,这种情况很少见,实际问题是,当我们对数组排序时,最好使用指针数组,而不是数组因此,任何一个好的答案都是主观的。同意没有容器的整个结构,使排序不那么昂贵。我不能谈论C++中数组的惯用用法,但它不鼓励在STL容器中使用(裸)指针,因为对象的寿命更难跟踪。(当需要指针时,建议使用shared_ptr,例如,为了避免切片或防止大量对象复制(如本例),或者当与不可复制类型一起使用时。)事实上,在这一点上,AFAIK qsort仅可用于POD类型(标准似乎对此主题没有提及,但谷歌搜索“qsort可用于非POD类型”很有启发性)。OP的示例在结构中使用了一些非POD类型(特别是std::string),因此这是另一个支持保留指针而不是整个对象的参数。(但是,像shared_ptr这样的智能指针也不是POD类型,因此无论如何,通常不建议使用qsort。)
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
}
qsort(c, Count, sizeof(CustomerProfile), CompareCustomerProfiler).
std::sort( customers, customers+Count, &customer_sorter);
#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;
}