C++ 优先队列中的多重比较

C++ 优先队列中的多重比较

我们知道优先队列具有先进先出的功能,但它的基本实现还附带了一些优先性。在C++标准模板库的优先队列中,我们可以通过简单的C++代码片段和一个容器轻松实现它。在这里,我们将通过带有注释的代码来理解C++优先队列中的多重比较并获得相关输出。

C++代码

// here we are writing down the C++ programming language code
// to demonstrate the concept of Multiple comparisons in a C++ 
// priority queue?
#include 
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 
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, greater > 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, 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, 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;
}

输出:

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++代码

// here we are writing down the c++ programming language code
// to demonstrate the concept of Multiple comparisons in a C++ 
// priority queue?
#include 
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, 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;
}

输出:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程