Multiple Comparisons In A C++ Priority Queue

We know that the priority queue is the same with the first in, first out functionality, but some priority is attached to its basic implementation. In the C++ standard template library priority queue, we implemented it easily with a simple C++ code snippet and as a container. Here, we will see the Multiple comparisons in a C++ priority queue with its code supported by comments to understand their relevant outputs better.

C++ Code

Example

// here we are writing down the C++ programming language code
// to demonstrate the concept of Multiple comparisons in a C++ 
// priority queue?
#include <bits/stdc++.h>
using namespace std;

// this is the basic template to implement the priority queue 
// written as void function without any return type as i
// general, it prints the heap, again and again, any data type
template <class T>
void printQueue(T& q)
{
    // simple while loop to implement the priority queue
	while (q.empty() == false) {
		cout << q.top() << " ";
		q.pop();
	}
	cout << endl;
}

// this is the basic priority queue property to sort in the 
// form of smallest to highest to the queue
void samplepriorityqueue()
{
	priority_queue<int, vector<int>, greater<int> > pq;
	for (int i = 1; i < 13; i++) {
		pq.push(i);
	}
	printQueue(pq);
}

// Here we are using the lambda comparator function
// to sort given priority queue in ascending order
void samplepriorityqueue1()
{
	auto compare = [](int left, int right) {
		return left < right;
	};
	priority_queue<int, vector<int>, decltype(compare)> pq(
		compare);
	for (int i = 1; i < 13; i++) {
		pq.push(i);
	}
	printQueue(pq);
}

// the struct keyword helps us with implementing the mycmp
// sorting the priority queue, this function has a flexibility 
// extend to various useful features 
struct mycmp {
	bool operator()(int a, int b)
	{
		return a > b;
	}
};
// below void function is the simple code snippet to implement
// the priority queue number two
void samplepriorityqueue2()
{
	priority_queue<int, vector<int>, mycmp> pq;
	// simple for the loop code snippet to run the loop pushing
	// the elements to the priority queue
	for (int i = 1; i < 13; i++) {
		PQ.push(i);
	}
	printQueue(pq);
}

// The main driver code functionality starts from here
int main()
{
    // the driver contains nothing but only function calls
    // which implements the priority queue and prints the 
    // output on our display screens
	samplepriorityqueue();
	samplepriorityqueue1();
	samplepriorityqueue2();
	return 0;
}

Output:

Output

1 2 3 4 5 6 7 8 9 10 11 12 
12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12

C++ Code

Example

// here we are writing down the c++ programming language code
// to demonstrate the concept of Multiple comparisons in a C++ 
// priority queue?
#include <bits/stdc++.h>
using namespace std;

// Structure of a user-defined data structure
struct jobs {
public:
	int priority;
	int processing;
	int arrival time;
	int proccessing_time;
	string job;
	jobs(int priority1, int processing1, int arrivaltime1,
		int proccessing_time1, string s1)
	{
		this->priority = priority1;
		this->processing = processing1;
		this->arrivaltime = arrivaltime1;
		this->proccessing_time = proccessing_time1;
		this->job = s1;
	}
};

// to tackle the processing time, we sort the priority queue
// let us say if the priority queue is similar and we have sorted 
// it in order of increasing to decreasing in the case of the same 
// priority if that is not the case, we have to sort it in the 
// ascending order depending on the priority thrown at us!
struct compare {
	bool operator()(jobs a, jobs b)
	{
		if (a.priority == b.priority) {
			return a.processing < b.processing;
		}
		return a.priority > b.priority;
	}
};

// The main driver code functionality starts from here
int main()
{
	priority_queue<jobs, vector<jobs>, compare> pq;
	// simple for loop to implement the priority queue named jobs
	for (int i = 0; i < 10; i++) {
		jobs a(rand() % 11, i + 1, rand() % 3 + i,
			rand() % 5 + 3, "my_work_day_job_is" + to_string(i));
		PQ.push(a);
	}

	while (pq.empty() == false) {
		jobs b = PQ.top();
		PQ.pop();
		  // the driver contains the while loop 
         // which implements the priority queue and prints the 
        // output on our display screens

		cout << b.priority << " " << b.processing << " "
			<< b.arrivaltime << " " << b.proccessing_time
			<< " " << b.job << endl;
	}
	return 0;
}

Output:

Output

0 9 10 5 my_work_day_job_is8
0 8 7 6 my_work_day_job_is7
3 3 2 4 my_work_day_job_is2
4 2 3 3 my_work_day_job_is1
6 1 1 6 my_work_day_job_is0
7 5 5 3 my_work_day_job_is4
7 4 5 4 my_work_day_job_is3
10 10 10 6 my_work_day_job_is9
10 7 7 5 my_work_day_job_is6
10 6 5 4 my_work_day_job_is5

Input Required

This code uses input(). Please provide values below: