Priority Queue,Using custom compare function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • osfreak
    New Member
    • Oct 2008
    • 22

    Priority Queue,Using custom compare function

    Code:
    #include<queue>
    #include<iostream>
    using namespace std;
    class CA
    {
    public:
    	CA(int parm,int pri):data(parm),priority(pri)
    	{}
    	CA()
    	{}
    
    	
    	bool operator < (CA rhs)
    	{
    		return data < rhs.GetData();
    	}
    
    	bool pricheck (const CA rhs)
    	{
    		return priority < rhs.GetPriority();
    	}
    	
    
    	int GetData()
    	{
    		return data;
    	}
    	int GetPriority()
    	{
    		return priority;
    	}
    private:
    	int data;
    	int priority;
    };
    
    
    int main()
    {
    	priority_queue<CA> myq(&CA::pricheck); // Use custom compare function
    	
    	
    	myq.push(CA(1,2));//(element,priority)
    	myq.push(CA(2,4));
    	myq.push(CA(3,5));
    	myq.push(CA(4,6));
    	myq.push(CA(5,1));
    	myq.push(CA(6,3));
    
            //Pop by priority
    	myq.pop();	//pop 5
    	myq.pop();	//pop 1
    	myq.pop();	//pop 6
    	myq.pop();	//pop 2
    	myq.pop();	//pop 3
    
    	
    	myq.empty();
    	return 0;
    }
    I get these errors,

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

    main.cpp(40) : error C2664: 'std::priority_ queue<_Ty>::pri ority_queue(con st _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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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:

    Code:
    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:

    Code:
     struct CALess : public binary_function <CA, CA, bool> 
      {
         bool operator ()(const CA& lhs, CA& rhs) const
    	 {
             return lhs < rhs;
    	 }
    };
    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::operato r() calls the CA::operator< to do the actual compare.

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

    Comment

    • osfreak
      New Member
      • Oct 2008
      • 22

      #3
      That was one very clear explanation...

      Looks easier now..

      Thank you

      Comment

      • gurudatha
        New Member
        • May 2014
        • 1

        #4
        Thankyou for the post.

        Comment

        Working...