为什么运算符< 在c++中实现基于类的优先级队列时需要重载吗?

发布于 2024-07-15 11:55:56 字数 467 浏览 11 评论 0 原文

请注意,我并不是在寻求答案。 我只是很好奇为什么事情会起作用

我需要为打印机模拟器实现一个优先级队列以进行班级分配。 在查看了互联网上的示例后,我注意到运算符< 为了正确安排优先级队列而超载。

相关代码:java2s 优先级队列示例

为什么运算符< > 需要超载吗? “<”在哪里 甚至用来进行比较? 实现运算符重载是否会改变队列 STL 的工作方式?

这个实现对我来说根本不直观:为什么不使用operator> 反而超载了? 应该如何学习该运算符< 需要重载才能使priority_queue正常工作?

note, I am not asking for answers. I simply am curious regarding why things work

I need to implement a priority queue for a printer simulator for a class assignment. After looking at examples on the internet, I noticed that operator< was being overloaded in order to arrange the priority queue correctly.

code in question: java2s priority queue example

Why does operator< need to be overloaded? Where is '<' even used to make the comparison? Does implementing the operator overload change the way the queue STL works?

This implementation doesn't seem intuitive to me at all: why isn't operator> being overloaded instead? How is one supposed to learn that operator< needs to be overloaded in order for the priority_queue to work correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蘑菇王子 2024-07-22 11:55:56

STL容器使用运算符< 对于那些订购内容的容器,默认情况下订购内容。

您可以通过将比较函子传递给容器的构造函数来覆盖它,这允许您将排序/顺序与容器对象解耦。

运算符> 本来可以选择,但必须选择一个,那就是operator<,然后在各处使用以保持一致性。

STL containers use operator< by default to order the contents, for those containers that order the contents.

You can override this by passing in a comparison functor to the constructor of the container, which allows you to decouple the sorting/ordering from the container object.

Operator> could have been chosen, but one had to be picked and that was operator<, and is then used everywhere for consistency.

时光清浅 2024-07-22 11:55:56

为什么运算符< 需要超载吗?

priority_queueCompare 函数对象,比较>

Compare 在其参数类型上引入严格的弱排序,如 LessThan Comparable 要求中所定义。

LessThanComparable 文档

注释

1 仅运算符< 是根本性的; 其他不等式运算符本质上是语法糖。

2 反对称性是一个定理,而不是一个公理:它遵循非自反性和及物性。

[3] 由于非自反性和传递性,运算符< 始终满足偏序的定义。 严格弱排序的定义更严格,全排序的定义更严格。

“<”在哪里 甚至用来进行比较?

void push(const value_type& x) 向队列中插入一个值。

实现运算符重载是否会改变队列 STL 的工作方式?

是的当然。 如果在比较中交换元素的顺序,则排序会采用相反的方式。

Why does operator< need to be overloaded?

The Compare function object in priority_queue<T, Sequence, Compare>:

Compare induces a strict weak ordering, as defined in the LessThan Comparable requirements, on its argument type.

LessThanComparable documentation:

Notes

1 Only operator< is fundamental; the other inequality operators are essentially syntactic sugar.

2 Antisymmetry is a theorem, not an axiom: it follows from irreflexivity and transitivity.

[3] Because of irreflexivity and transitivity, operator< always satisfies the definition of a partial ordering. The definition of a strict weak ordering is stricter, and the definition of a total ordering is stricter still.

Where is '<' even used to make the comparison?

void push(const value_type& x) which inserts a value in the queue.

Does implementing the operator overload change the way the queue STL works?

Yes, of course. If you swap the order of elements in the comparison, your sort goes the other way.

傾城如夢未必闌珊 2024-07-22 11:55:56

priority_queue 使用 std::less,这又需要 T() T() 是一个有效的表达式。 可以是会员,也可以是非会员,但表达式必须有效。

但是, std::less 可能是专门化的,因此您的声明运算符< 需要超载有点误导。

priority_queue<T> uses std::less<T>, which in turn requires T() < T() to be a valid expression. Could be a member, could be a non-member, but the expression has to be valid.

However, std::less<T> may be specialized, so your statement that operator< needs to be overloaded is a bit misleading.

挥剑断情 2024-07-22 11:55:56

好的,基本原因是优先级队列是一种结构,其中插入的项目按照某个顺序函数的顺序返回。 您需要一个排序,处理它的明显方法是重载运算符<。 您可以按名称拥有一个函数,但能够说 if( a < b) 可以说比 if(isLessThan(a,b)) 或其他内容更具可读性像那样。

您不会重载operator>,因为它不是必需的; 优先级队列中唯一需要的操作是小于。 这并不意味着您不能拥有一个,但由于您有一个==,您可以简单地实现它——或者只是反转操作数。

Okay, the basic reason is that a priority queue is a structure in which inserted items are returned in the order of some order function. You need an ordering and the obvious way to handle it is by overloading operator<. You could have a function by name, but being able to say if( a < b) is arguably more readable than if(isLessThan(a,b)) or something like that.

You don't overload operator> because it isn't needed; the only operation you need in a priority queue is less-than. That doesn't mean you couldn't have one, but since you have an ==, you can implement it trivially -- or just reverse the operands.

笑看君怀她人 2024-07-22 11:55:56

仅供参考:std::priority_queue 可以接受比较谓词。
运算符< 用作默认行为。 这是最低要求。

为什么不是运营商> 超载
相反

我认为是历史原因。 在数学中,所有示例通常描述为< 操作,一个运算符足以定义所有其他操作(>、<=、>=、==、!=)。

这个实现似乎没有
对我来说完全直观

对于我来说,这个界面是预料之中的。 我认为这是一种习惯。

是否实现运算符
重载改变队列STL的方式
有效吗?

不,不。 不是 STL 队列,只有您的队列 - 对于您的类型,如果您自己的比较器未定义。

FYI: std::priority_queue could accept Compare predicate.
operator< used just as default behaviour. It is minimal requirement.

why isn't operator> being overloaded
instead

I think by historical reaqsons. In mathematic all examples usualy described for < operation, and one operator is enought for define all other operation ( >, <=, >=, ==, !=).

This implementation doesn't seem
intuitive to me at all

For me this interface was expected. I think it as habbit.

Does implementing the operator
overload change the way the queue STL
works?

No, no. Not STL queue, only your queue - for your type, if your own comparator was not defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文