请注意,我并不是在寻求答案。 我只是很好奇为什么事情会起作用
我需要为打印机模拟器实现一个优先级队列以进行班级分配。 在查看了互联网上的示例后,我注意到运算符< 为了正确安排优先级队列而超载。
相关代码: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?
发布评论
评论(5)
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.
priority_queueCompare
函数对象,比较>:LessThanComparable 文档:
void push(const value_type& x)
向队列中插入一个值。是的当然。 如果在比较中交换元素的顺序,则排序会采用相反的方式。
The
Compare
function object inpriority_queue<T, Sequence, Compare>
:LessThanComparable documentation:
void push(const value_type& x)
which inserts a value in the queue.Yes, of course. If you swap the order of elements in the comparison, your sort goes the other way.
priority_queue
使用std::less
,这又需要T()
T()
是一个有效的表达式。 可以是会员,也可以是非会员,但表达式必须有效。但是,
std::less
可能是专门化的,因此您的声明运算符< 需要超载有点误导。priority_queue<T>
usesstd::less<T>
, which in turn requiresT() < 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.好的,基本原因是优先级队列是一种结构,其中插入的项目按照某个顺序函数的顺序返回。 您需要一个排序,处理它的明显方法是重载运算符<。 您可以按名称拥有一个函数,但能够说
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 thanif(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.仅供参考:std::priority_queue 可以接受比较谓词。
运算符< 用作默认行为。 这是最低要求。
我认为是历史原因。 在数学中,所有示例通常描述为< 操作,一个运算符足以定义所有其他操作(>、<=、>=、==、!=)。
对于我来说,这个界面是预料之中的。 我认为这是一种习惯。
不,不。 不是 STL 队列,只有您的队列 - 对于您的类型,如果您自己的比较器未定义。
FYI: std::priority_queue could accept Compare predicate.
operator< used just as default behaviour. It is minimal requirement.
I think by historical reaqsons. In mathematic all examples usualy described for < operation, and one operator is enought for define all other operation ( >, <=, >=, ==, !=).
For me this interface was expected. I think it as habbit.
No, no. Not STL queue, only your queue - for your type, if your own comparator was not defined.