By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Priority Queue,Using custom compare function

osfreak
P: 22
Expand|Select|Wrap|Line Numbers
  1. #include<queue>
  2. #include<iostream>
  3. using namespace std;
  4. class CA
  5. {
  6. public:
  7.     CA(int parm,int pri):data(parm),priority(pri)
  8.     {}
  9.     CA()
  10.     {}
  11.  
  12.  
  13.     bool operator < (CA rhs)
  14.     {
  15.         return data < rhs.GetData();
  16.     }
  17.  
  18.     bool pricheck (const CA rhs)
  19.     {
  20.         return priority < rhs.GetPriority();
  21.     }
  22.  
  23.  
  24.     int GetData()
  25.     {
  26.         return data;
  27.     }
  28.     int GetPriority()
  29.     {
  30.         return priority;
  31.     }
  32. private:
  33.     int data;
  34.     int priority;
  35. };
  36.  
  37.  
  38. int main()
  39. {
  40.     priority_queue<CA> myq(&CA::pricheck); // Use custom compare function
  41.  
  42.  
  43.     myq.push(CA(1,2));//(element,priority)
  44.     myq.push(CA(2,4));
  45.     myq.push(CA(3,5));
  46.     myq.push(CA(4,6));
  47.     myq.push(CA(5,1));
  48.     myq.push(CA(6,3));
  49.  
  50.         //Pop by priority
  51.     myq.pop();    //pop 5
  52.     myq.pop();    //pop 1
  53.     myq.pop();    //pop 6
  54.     myq.pop();    //pop 2
  55.     myq.pop();    //pop 3
  56.  
  57.  
  58.     myq.empty();
  59.     return 0;
  60. }
  61.  
I get these errors,

main.cpp(20) : error C2662: 'CA::GetPriority' : cannot convert 'this' pointer from 'const CA' to 'CA &

main.cpp(40) : error C2664: 'std::priority_queue<_Ty>::priority_queue(const _Pr &)' : cannot convert parameter 1 from 'bool (__thiscall CA::* )(const CA)' to 'const std::less<_Ty> &'

The objective is to maintain the queue by priority, while retaining the comparison operator overloading for data comparison.

I need some help with this,


p.s- Am using studio 2008
Oct 13 '10 #1

✓ answered by weaknessforcats

CA::pricheck has a const CA argument. It calls CA::GetPriority which returns an int.

However, CA::GetPriority might change the member variables so it can't be called using a const CA.

You need to make CA::GetPriority a const member function.

Next, you need to create your priority_queue object correctly:

Expand|Select|Wrap|Line Numbers
  1. priority_queue< CA, vector<CA>, CALess > myq;
The template is a container-adapter. That means it uses a container (in this case a vector) as your container.
The custom compare is the 3rd template parameter, not the second.

Next, your custom compare must be a binary predicate. priority_queue defaults to less<>.

So you create your own binary predicate. (A binary predicate a) derives from binary_function and B)takes two objects of the class type and returns a bool.

Here is an example:

Expand|Select|Wrap|Line Numbers
  1.  struct CALess : public binary_function <CA, CA, bool> 
  2.   {
  3.      bool operator ()(const CA& lhs, CA& rhs) const
  4.      {
  5.          return lhs < rhs;
  6.      }
  7. };
In this example, a CALess object implements the function operator that takes two CA objects and calls CA::operator<. This type of object is called a functor.

Internally, priority_queues calls: CALess(arg1, arg2).

That is, it uses the CALess object as a function. The CALess::operator() calls the CA::operator< to do the actual compare.

Note that CALess::operator() can perform any logic whatsoever as long as it returns a bool.

Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
CA::pricheck has a const CA argument. It calls CA::GetPriority which returns an int.

However, CA::GetPriority might change the member variables so it can't be called using a const CA.

You need to make CA::GetPriority a const member function.

Next, you need to create your priority_queue object correctly:

Expand|Select|Wrap|Line Numbers
  1. priority_queue< CA, vector<CA>, CALess > myq;
The template is a container-adapter. That means it uses a container (in this case a vector) as your container.
The custom compare is the 3rd template parameter, not the second.

Next, your custom compare must be a binary predicate. priority_queue defaults to less<>.

So you create your own binary predicate. (A binary predicate a) derives from binary_function and B)takes two objects of the class type and returns a bool.

Here is an example:

Expand|Select|Wrap|Line Numbers
  1.  struct CALess : public binary_function <CA, CA, bool> 
  2.   {
  3.      bool operator ()(const CA& lhs, CA& rhs) const
  4.      {
  5.          return lhs < rhs;
  6.      }
  7. };
In this example, a CALess object implements the function operator that takes two CA objects and calls CA::operator<. This type of object is called a functor.

Internally, priority_queues calls: CALess(arg1, arg2).

That is, it uses the CALess object as a function. The CALess::operator() calls the CA::operator< to do the actual compare.

Note that CALess::operator() can perform any logic whatsoever as long as it returns a bool.
Oct 13 '10 #2

osfreak
P: 22
That was one very clear explanation...

Looks easier now..

Thank you
Oct 15 '10 #3

P: 1
Thankyou for the post.
May 4 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.