-3
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;
}
我可以根據更大的計數值對地圖進行排序,如果計數相等,則基於較低的索引值?如何基於C++中的值使用stl對映射進行排序?
一種方法是推送矢量中的所有值並執行排序。有沒有其他方式可以使用優先隊列進行排序,如下所示?
priority_queue<pair<int,struct node *>, vector<int,struct node *>, compare> pq(m1.begin(),m1.end());
我已經提供了上面的比較功能。
'map'由鍵排序,不是值... – Jarod42
的地圖已經排序,和它使用的密鑰(你的情況'int')要做到這一點排序。 – CoryKramer
你不能'排序'一張地圖。 – NathanOliver