[英]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 c++17 , so you can use auto
for template parameters, so:您已将问题标记为c++17 ,因此您可以将
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.