Operator < for priority queue | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Operator < for priority queue

Hi https://code.sololearn.com/c2m1Xu61FvMQ I am getting proper output from priority_queue with existing code for urgent jobs. I mean all urgent jobs are on top most of queue. What to do as I don't want to change order of non urgent jobs. To be specific , consider below example: 1 -->Not urgent 3 -->Not urgent 9 -->Not urgent 2 -->Not urgent 4 -->urgent 5 -->Not urgent 0 -->urgent 6 -->Not urgent 8 -->urgent should be set into queue as per below: 4 -->urgent 0 -->urgent 8 -->urgent 1 -->Not urgent 3 -->Not urgent 9 -->Not urgent 2 -->Not urgent 5 -->Not urgent 6 -->Not urgent*/

4th Jun 2022, 6:26 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 5
Indirectly you want the ordering to be based on object's creation time if they have same "urgency" ( both are urgent or both are not urgent ) One solution to do that is to keep track of the creation time of the object in a seperate member variable and initialise it with an incrementing sequence number ( maybe a static member ) Now that we have record of creation time of the object, we can simply use it as secondary key for our ordering in "<" operator. Here is a quick fix implementing the same👇 https://code.sololearn.com/cOVpXJfnxpuv/?ref=app
5th Jun 2022, 1:51 AM
Arsenic
Arsenic - avatar