Блог пользователя red_coder

Автор red_coder, 12 лет назад, По-английски

Hey guys i want to implement a priority queue which contains entries of type 'node'. The code is as follows....

struct node
{
    int a;
    int b;
};

bool cmp(const node& A,const node& B)
{
    return (A.a<B.a) || (A.a==B.a && A.b>B.b);
}

/// main function starts 

 priority_queue<node,vector<node>, cmp> Y;

But i am not able to compile with error... "type/value mismatch at argument 3 in template parameter list for 'template<class _Tp, class _Sequence, class _Compare> class std::priority_queue"... Can anyone help me out...

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
12 лет назад, # |
Rev. 2   Проголосовать: нравится +14 Проголосовать: не нравится

The third template parameter of priority_queue expects a type (_functor_), not a function. Rather than doing it this way, it's easier to define the comparison operator inside the struct, then priority_queue will see it automatically:

struct node
{
    int a;
    int b;

    bool operator <(const node& other) const
    {
        return (a<other.a) || (a==other.a && b>other.b);
    }
};

. . .

priority_queue<node> Y;

If you still want to use the functor way, then you should define the functor class:

struct node
{
    int a;
    int b;
};

struct cmp
{
    bool operator ()(const node& A,const node& B) const
    {
        return (A.a<B.a) || (A.a==B.a && A.b>B.b);
    }
};

. . .

priority_queue<node,vector<node>, cmp> Y;