C++结构排序错误
我尝试
struct Book{
public:int H,W,V,i;
};
使用简单函子
class CompareHeight
{
public:
int operator() (Book lhs,Book rhs)
{
return lhs.H-rhs.H;
}
};
在尝试时
vector<Book> books(X);
.....
sort(books.begin(),books.end(), CompareHeight());
对 C++ 中的自定义结构向量进行排序:它给了我异常“无效运算符 <”
这个错误是什么意思?
谢谢
I am trying to sort a vector of custom struct in C++
struct Book{
public:int H,W,V,i;
};
with a simple functor
class CompareHeight
{
public:
int operator() (Book lhs,Book rhs)
{
return lhs.H-rhs.H;
}
};
when trying :
vector<Book> books(X);
.....
sort(books.begin(),books.end(), CompareHeight());
it gives me exception "invalid operator <"
What is the meaning of this error?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sort
需要一个返回bool
的函数,当且仅当左侧在右侧之前时,该函数为 true:另请注意对
const Book&
参数的更改,以避免复制。sort
expects a function that returnsbool
, which is true iff the lhs precedes the rhs:Also note the change to
const Book&
parameters, to avoid copying.