简体   繁体   English

如何使用一个 function 按结构中的不同变量排序

[英]How can I use one function to sort by different variables in a struct

In my program, I have this struct:在我的程序中,我有这个结构:

struct Record {
string ID;
string name;
int quantity;
double price; 
};

I also have a function that is supposed to bubble sort an array of pointers to Record that looks like this:我还有一个 function 应该对指向 Record 的指针数组进行冒泡排序,如下所示:

void printAscending(Record* pitemList[50], int arraySize) {
  int Swap;
  Record* Temp;
  do{
    Swap = 0;
    for(int i = 0; i < (arraySize - 1); i++) {
      if ((pitemList[i] -> quantity) > (pitemList[i + 1] -> quantity)){
        Temp = pitemList[i];
        pitemList[i] = pitemList[i + 1];
        pitemList[i + 1] = Temp;
        Swap = 1;
      }
    }
  } while (Swap != 0);
}

There are four different variables in struct and I have to ask a user to pick one to sort by. struct 中有四个不同的变量,我必须让用户选择一个进行排序。 I can't create four different functions for each variable, and I also can't copy and paste the same bubble sort code four times for each variable.我不能为每个变量创建四个不同的函数,也不能为每个变量复制和粘贴相同的冒泡排序代码四次。 Is there any way to create one function with one block of bubble sort code that can sort by four different variables?有没有办法用一组可以按四个不同变量排序的冒泡排序代码创建一个 function?

Not sure I understand what you want, but to generalize your code to use a different member of the records as a comparator, I suppose you can pass pointers to members as template parameters.不确定我理解你想要什么,但是为了概括你的代码以使用记录的不同成员作为比较器,我想你可以将指向成员的指针作为模板参数传递。

You've tagged your question as , so you can use auto for template parameters, so:您已将问题标记为 ,因此您可以将auto用于模板参数,因此:

template <auto Record::* rMember>
void printAscending (Record* pitemList[50], int arraySize)
 {
   bool Swap;

   do
    {
      Swap = false;

      for (int i = 0; i < (arraySize - 1); i++)
       {
         if ( pitemList[i]->*rMember > pitemList[i + 1]->*rMember )
          { 
            std::swap(pitemList[i], pitemList[i+1]);

            Swap = true;
          }
       }
    }
   while ( true == Swap );
 }

You can call it as follows:你可以这样称呼它:

printAscending<&Record::quantity>(itemList, sizeItemList);

I am writing this from the top of my head but I think that this is the step in the right way.我是从头顶上写的,但我认为这是正确的一步。

You would need to have few functions like this:你需要有几个这样的功能:

bool compareByQuantity(const Record &a, const Record &b) {
    return a.quantity < b.quantity;
}

You would need to implement this for every property.您需要为每个属性实现此功能。 And then, to sort this, you can provide third argument to sort method... third argument is function.然后,要对此进行排序,您可以为排序方法提供第三个参数......第三个参数是 function。 like this像这样

std::sort(records.begin(), records.end(), compareByQuantity);

Hope this gets you on the right track希望这能让你走上正确的轨道

Link to sort documentation: https://en.cppreference.com/w/cpp/algorithm/sort排序文档链接: https://en.cppreference.com/w/cpp/algorithm/sort

As pointed out by @Ted Klein Bergman you can also use a lambda directly like this正如@Ted Klein Bergman 所指出的,您也可以像这样直接使用 lambda

std::sort(records.begin(), records.end(), [](const Record &a, const Record &b){ return a.quantity < b.quantity }); 

I think it would be better to use std::sort, which lets you write just your compare functions and takes care of the sorting.我认为使用 std::sort 会更好,它可以让您只编写比较函数并负责排序。 You could use these:你可以使用这些:

#include <algorithm>

bool compareIDs(const Record *r1, const Record *r2) { return r1->ID < r2->ID; }
bool compareNames(const Record *r1, const Record *r2) { return r1->name < r2->name; }
bool compareQuantities(const Record *r1, const Record *r2) { return r1->quantity < r2->quantity; }
bool comparePrices(const Record *r1, const Record *r2) { return r1->price < r2->price; }

// And call sort like this: std::sort(array1, array2, compareIDs);

References:参考:
https://en.cppreference.com/w/cpp/algorithm/sort https://en.cppreference.com/w/cpp/algorithm/sort
https://en.cppreference.com/w/cpp/named_req/Compare https://en.cppreference.com/w/cpp/named_req/Compare

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中的不同类中使用在一个类中定义的结构? - How can I use struct defined in one class in a different class in C++? 如何根据结构的字段之一对结构指针列表进行排序? - How can I sort a list of struct pointers according to one of the struct's fields? 我可以在自己的结构中使用向量排序 function 吗? 总是编译器错误 - Can I use vector sort function in my own struct? always compiler error 如果我想使用该结构中的字段作为参数,如何从结构内部使用递归 function - How can I use a recursive function from inside a struct if I want to use a field from that struct as a parameter 如何在不同的文件中使用结构 - How do I use a struct in a different file 如何在不同的 function 中访问存储在动态 memory 中的变量? - How can I access variables stored in the dynamic memory in a different function? 如何在一个 function 中返回两个 char* class 变量? - How can I return two char* class variables in one function? 如何为不同的模板参数定义不同的结构成员? - How can I define different struct members for different template parameters? 如何对多个函数或结构使用单个Template语句? - How to use a single Template statement for more than one function or struct? 对不同变量的向量使用排序函数 - Using the sort function on vectors on different variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM