一对<int、pair<int、int 的优先级队列的自定义比较器> >

Custom Comparator for Priority Queue of a pair<int, pair<int, int> >

本文关键字:int gt lt 比较器 自定义 pair 一对 优先级 队列      更新时间:2023-10-16
#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代码

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

应该改成:

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