struct node{
int index;
int count;
};
map<int,struct node *> m1;
bool compare(struct node* a, struct node* b) {
if(a->count>b->count)
return 1;
if(a->count==b->count && a->index<b->index)
return 1;
return 0;
}
Can i sort the map based on greater count value and if count is equal then based on lower index value?
A way is to push all the values in vector and perform sort. Is there any other way by which sorting can be done using priority queue as below?
priority_queue<pair<int,struct node *>, vector<int,struct node *>, compare> pq(m1.begin(),m1.end());
I have provided the compare function above.
Solution using priority_queue
class compare {
public:
bool operator()(pair<int,struct node *> a, pair<int,struct node *> b) {
if(a.second->count>b.second->count)
return 0;
if(a.second->count==b.second->count && a.second->index<b.second->index)
return 0;
return 1;
}
};
priority_queue<pair<int,struct node *>, vector<pair<int,struct node *> >, compare > pq(m1.begin(),m1.end());