473,508 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Priority Queue,Using custom compare function

osfreak
22 New Member
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
3 12925
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
22 New Member
That was one very clear explanation...

Looks easier now..

Thank you
Oct 15 '10 #3
gurudatha
1 New Member
Thankyou for the post.
May 4 '14 #4

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

Similar topics

4
5131
by: Bram Stolk | last post by:
Hi there, If you have a list of elements, you can sort it with an alternative comparator (default is __lt__()) This is done as: >>> l= >>> l.sort()
38
5733
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
5
13207
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
16
5374
by: Crirus | last post by:
hello I read somewhere about priority queue...what is that and Vb net have such class? -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it...
3
7034
by: eric.boissard | last post by:
Hello, I managed to implement the AStar algorithm, however I have some trouble using the priority queue, and the 'compare' function. Here is my 'Waypoint.h' header file: class Waypoint {...
4
10343
by: vfunc | last post by:
Is the STL priority queue a proper implementation of a heap with siftup algorithm etc ? How do you implement an STL priority queue (link to an example) ? Is there a resort method, why ? Thanks
2
2820
by: i am lost | last post by:
I have code that I am strugling to write could u plz help me with it ...I 'll be thankfull Implement a Priority Queue using Stacks. I am not allowed to use any other data structure. Example: ...
3
7399
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
4
3406
by: jjh5030 | last post by:
This is a programming assignment. You are asked to work with pointers. Be aware that error messages are often not very helpful when your pointers point to bad locations. Therefore, reserve...
14
16925
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.