2016-05-19 4 views

答えて

3

はい、Compareテンプレートパラメータpriority_queueを使用して独自のコンパレータを提供できます。デフォルトでは、Tのように特化されてペアに呼び出され<オペレータ、原因となる要素の型、あるstd::less<T>Compareデフォルト:

template <class T1, class T2> 
bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y) 
{ return x.first < y.first || (!(y.first < x.first) && x.second < y.second) } 

はここで別のコンパレータを使用する方法の例です:

#include <queue> 
#include <utility> 
#include <vector> 

using namespace std; 

struct CompareByFirst { 
    constexpr bool operator()(pair<int, int> const & a, 
           pair<int, int> const & b) const noexcept 
    { return a.first < b.first; } 
}; 

int main() { 
    priority_queue<pair<int, int>, 
        std::vector<pair<int, int> >, 
        CompareByFirst> myQueue; 
} 
関連する問題