red_coder's blog

By red_coder, 12 years ago, In English

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...

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
12 years ago, # |
Rev. 2   Vote: I like it +14 Vote: I do not like it

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;
  • »
    »
    12 years ago, # ^ |
    Rev. 2   Vote: I like it -21 Vote: I do not like it

    excellent... thanks a lot.. Can i get ur facebook id or email id so that i can be in touch with u ... :)

    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it +7 Vote: I do not like it

      There is a contact option on CodeForces too (send message to user on profile page):P