Why does the <operator need to be overloaded when implementing priority queues at the class level in C ++?

Notice I am not asking for an answer. I'm just wondering why everything works

I need to implement a priority queue for a printer simulator for class assignment. After looking at the examples on the Internet, I noticed that the <operator was overloaded to properly prioritize.

code in question: java2s priority queue example

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

This implementation does not seem intuitive to me: why is the operator not overloaded? How can we assume that the <operator needs to be overloaded in order for priority to work correctly?

+3
5

STL < , .

, , / .

p > , , <, .

+9

< ?

Compare priority_queue<T, Sequence, Compare>:

Compare , LessThan Comparable, .

LessThanComparable:

1 < ; .

2 - , : .

[3] - < . , .

'<' ?

void push(const value_type& x), .

, STL ?

, . , .

+4

priority_queue<T> std::less<T>, , , T() < T(). , , .

std::less<T> , , < , .

+2

, , - , . , - <. , if( a < b), , , if(isLessThan(a,b)) - .

operator>, ; , , . , , ==, - .

+1

FYI: std:: priority_queue .
< . .

, . , < ( > , < =, > =, ==,! =).

For me, this interface was expected. I consider this a habit.

Does the operator overload reorder the queue STL is working?

No no. Not the STL queue, only your turn is for your type if your own comparator is not defined.

+1
source

Source: https://habr.com/ru/post/1705386/


All Articles