473,666 Members | 2,225 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::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
Oct 13 '10 #1
3 12950
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::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.
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
5144
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
5773
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 queue. However, no matter what reasonably-sized type I use for the stamp, eventually the stamp will 'wrap around' and possibly cause incorrect ordering of elements. For my application, which queues elements very quickly and runs for an...
5
13234
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 enqueue, I do a binary search on where to put the object and then insert using that index and arraylist.insert(index, obj) The bottom of my arraylist is always my lowest, therefore, dequeueing is very fast and effecient.
16
5401
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 all from you
3
7041
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 { public: int node_id;
4
10360
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
2868
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: Class PriorityQueue { Stack PQ; … ….
3
7416
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 ( maximum element ) while set and heap can erase any element ...
4
3421
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 additional time for debugging. Implement a data structure Extended Queue using pointers. In addition to the usual queue operations Enqueue(x), Dequeue and the trivial operations Make-empty-queue and the Is-empty test, you are asked to have two...
14
16953
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 priority queue, which is an array heap, and for example i/2 is the father of i and so on.. the code of the static priority queue is this: int i= ++CurrentSize; while(i != 1 && X > heap) { // cannot put x in heap heap = heap; //move...
0
8443
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8550
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.