[英]C++: How to sort a vector of objects derived from class using one of its parameters
I'm trying to sort a vector of objects, created from a class in the project.我正在尝试对从项目中的类创建的对象向量进行排序。
I have two classes - Playlist and ListIndex, that inherits from a List_base class.我有两个类 - Playlist 和 ListIndex,它们继承自 List_base 类。 In the List_base class, I want to sort a vector of objects (either Playlist or ListIndex) by their name.
在 List_base 类中,我想按名称对对象向量(播放列表或 ListIndex)进行排序。 the name property is defined in the List_base class.
name 属性在 List_base 类中定义。 I tried using struct or function that compares the name field and pass it to the sort function.
我尝试使用 struct 或 function 来比较 name 字段并将其传递给 sort 函数。 I'm getting all kinds of errors.
我收到各种错误。 I'm new to C++ and stuck in this error for a very long time
我是 C++ 的新手,很长一段时间都陷入这个错误
The List_base sort method with the compare function带有比较函数的 List_base 排序方法
//the function should be able to get either Playlist vector
// or ListIndex vector (both inherits from List_base)
void List_base::sortList(vector<List_base> data) {
sort(data.begin(), data.end(), compFunc());
}
bool List_base::compFunc(List_base *a, List_base *b) {
return a->getName().compare(b->getName()) > 0;
}
The name field is declared in the List_base class: name 字段在 List_base 类中声明:
class List_base
{
public:
List_base(string name);
string getName();
void sortList(vector<List_base> data);
virtual ~List_base();
protected:
private:
string name;
bool compFunc(List_base *a, List_base *b);
};
What am I doing wrong?我究竟做错了什么? I can't even focus on a specific error.
我什至不能专注于一个特定的错误。 I've also tried casting but failed there too
我也试过铸造,但也失败了
pls, help!请帮助!
std::sort
expects the comparator you pass to be some callable type, whether that's a function pointer, functor, lambda, etc. It just needs something that can be called with a signature like bool compare(const T& left, const T& right)
. std::sort
期望您传递的比较器是某种可调用类型,无论是函数指针、函子还是 lambda 等。它只需要可以使用像bool compare(const T& left, const T& right)
这样的签名来调用的东西.
The first problem is that passing compFunc()
is calling the function compFunc
.第一个问题是传递
compFunc()
是调用函数compFunc
。 This should fail because compFunc
expects two arguments.这应该会失败,因为
compFunc
需要两个参数。 You should pass the function itself, not its return value:您应该传递函数本身,而不是它的返回值:
sort(data.begin(), data.end(), compFunc);
The second problem is that in your compare function you should accept the arguments by reference-to-const, not by pointer:第二个问题是,在您的比较函数中,您应该通过引用来接受参数,而不是通过指针:
bool compFunc(const List_base& a, const List_base& b);
where List_base
is exactly the element type you're sorting.其中
List_base
正是您要排序的元素类型。
The third problem is that compFunc
is a non-static member function .第三个问题是
compFunc
是一个非静态成员函数。 This means that this function depends on a List_base
instance to be used, and you need a secret this
parameter to call it, as in this->compFunc(...)
.这意味着该函数依赖于要使用的
List_base
实例,并且您需要一个秘密的this
参数来调用它,如this->compFunc(...)
。 You should instead make it static
or non-member, so that it can be treated as a normal function.您应该改为将其设为
static
或非成员,以便将其视为普通函数。
class List_base {
/* ... */
static bool compFunc(const List_base& a, const List_base& b);
};
bool List_base::compFunc(const List_base& a, const List_base& b){
return a.name < b.name;
}
Your sorting should work at this point.此时您的排序应该有效。 If you want to use the sorted result outside of
List_base::sortList
, you should accept data
by reference , so that the changes you make can be seen by the caller.如果您想在
List_base::sortList
之外使用排序结果,您应该通过引用接受data
,以便调用者可以看到您所做的更改。 Currently, sortList
accepts the list by value, meaning that it always receives a copy of what you pass it.目前,
sortList
按值接受列表,这意味着它始终会收到您传递给它的内容的副本。
Perhaps the cleanest fix to all this would be to the "less-than" operator ( <
), which the standard library uses by default in many cases to compare user-defined types.也许最干净的解决方法是使用“小于”运算符 (
<
),标准库在许多情况下默认使用它来比较用户定义的类型。 This could like something like the following:这可能类似于以下内容:
class List_base {
/* ... */
friend bool operator<(const List_base& a, const List_base& b){
return a.name < b.name;
}
};
with this, you don't need to explicitly specify your own comparator, and sorting is now as simple as有了这个,你不需要明确指定你自己的比较器,排序现在就像
sort(data.begin(), data.end());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.