How to update elements inside std :: priority_queue?

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <stack>
#include <stdio.h>
#include <list>
#include <string.h>
#include <queue>
#include <algorithm>
#define pb push_back
using namespace std;
typedef pair<int,int> ii;


struct node{
    int digit;
};


class Compare{
public:
    bool operator()(node* a,node* b){
        return (a->digit)>(b->digit);
    }
};


int main()
{
priority_queue<node*,vector<node*>,Compare> pq;
vector<node*> vec;
node* p = new node();
node* q = new node();
node* r = new node();
p->digit=100;
q->digit=200;
r->digit=300;
pq.push(p);
pq.push(q);
pq.push(r);
q->digit=50;
pq.push(nod);
while(!pq.empty()){
    cout<<(pq.top())->digit<<endl;
    pq.pop();
}
return 0;
}

I created a priority queue and inserted 3 nodes (struct) in the priority queue, and then I changed the value of the middle element present in the queue, but I canโ€™t figure out how to update the priority queue after updating the element?

0
source share
1 answer

The priority queue is designed to work with fixed priorities, i.e. the priority of the element must be known at the time of insertion, and then remain unchanged. This is why all comparisons are performed when an item is queued.

, , std::multimap. . begin() end() node .

, , , ++ 17 - .

0

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


All Articles