一对优先级队列的自定义比较器<int,pair <int,=“”int =“”>&gt; </int,>

时间:2011-07-08 03:46:27

标签: c++ compare priority-queue std-pair

#include <iostream>
#include <queue>
using namespace std;

template< typename FirstType, typename SecondType >
struct PairComparator {
  bool operator()( const pair<FirstType, SecondType>& p1, const pair<FirstType, SecondType>& p2 ) const 
    {  if( p1.first < p2.first ) return true;
        if( p2.first < p1.first ) return false;
        //return p1.second < p2.second;
    }
};

priority_queue<pair<int, pair<int, int> > > q;

int main() {
    q.push(make_pair(1, make_pair(2,3)));
    pair<int, pair<int, int> > a = q.top;
    return 0;
}

这给了我错误

pqpair.cpp:18: error: conversion from ‘<unresolved overloaded function type>’ 
to non-scalar type ‘std::pair<int, std::pair<int, int> >’ requested

我在cplusplus.com论坛找到了PairComparator代码

1 个答案:

答案 0 :(得分:3)

pair<int, pair<int, int> > a = q.top;

应改为:

pair<int, pair<int, int> > a = q.top();
                                    ^