2016-08-15 6 views
-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; 
} 

大きなカウント値に基づいてマップを並べ替えることができますか?カウントがより低いインデックス値に基づいて等しいかどうかを調べることはできますか?stlを使ってC++の値に基づいてマップをソートする方法は?

方法は、ベクトルのすべての値をプッシュし、並べ替えを実行することです。優先順位キューを使用してソートを行う方法は他にありますか?

priority_queue<pair<int,struct node *>, vector<int,struct node *>, compare> pq(m1.begin(),m1.end()); 

上記の比較機能を提供しました。

+5

'map'値によって... – Jarod42

+5

マップがすでにソートされていない、キーによってソートされ、それがこれを行うにはキー(あなたのケースでは、' int')を使用していますソート。 – CoryKramer

+0

マップを 'ソートできません。 – NathanOliver

答えて

0

ソリューション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()); 
0

ブーストMulti-Indexを試してみることができます。 1つの索引はマップ・アクセスを提供し、もう1つは順序付きイテレーター・アクセスを提供します。使用

関連する問題