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

Priority Queues and Structures

P: 16
Hello again, I was wondering if you could make priority queues with defined compare functions out of structs rather than objects. Currently I have this (with some things omitted:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. bool itemCompare(const item& a, const item& b) {
  4.     return a.number> b.number;
  5. }
  6.  
  7. void function(){
  8. .
  9. .
  10. .
  11.     priority_queue<item, vector<item>, boundCompare> itemQueue;
  12. .
  13. .
  14. }
Unfortunatly I get the error of:
bestKS.cpp:56: error: expected a type, got itemCompare

After doing some research I found this:

Expand|Select|Wrap|Line Numbers
  1. #include "event.h"
  2. #include <queue>
  3. using std::priority_queue;
  4.  
  5. class greater {
  6.   public: int operator() (Event *x, Event *y) { return *x > *y; }
  7. };
  8. priority_queue< Event *, vector<Event *>, greater > _event_queue;
  9.  
I tried making itemCompare a function in a class and calling on that class but yet that does not work either.

Thanks for the help!
May 29 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Kinda hard to say:
bestKS.cpp:56: error: expected a type, got itemCompare
since you don't supply line 56.

However, you can use a comparator as long as it is a binary predicate.

Be careful, though, as the name "greater" is already use by the STL. Odd you are not using it. If you just want to try your own, rename to "greaterThan" or some such.
May 29 '07 #2

P: 16
Kinda hard to say:


since you don't supply line 56.

However, you can use a comparator as long as it is a binary predicate.

Be careful, though, as the name "greater" is already use by the STL. Odd you are not using it. If you just want to try your own, rename to "greaterThan" or some such.
My mistake, with the code I omitted line 56 would be

priority_queue<item, vector<item>, itemCompare> itemQueue;

I am not using greater because it does not know how to compare two items. I want one item to be define as greater if item1.number > item2.number. I will keep working on this, thank you for the reply.
May 29 '07 #3

AdrianH
Expert 100+
P: 1,251
My mistake, with the code I omitted line 56 would be

priority_queue<item, vector<item>, itemCompare> itemQueue;

I am not using greater because it does not know how to compare two items. I want one item to be define as greater if item1.number > item2.number. I will keep working on this, thank you for the reply.
Well, you could overload operator <() in your class as that is what is used by default. Or if you want the order reversed use STL's greater and overload operator>().

See here for more info.


Adrian
May 29 '07 #4

P: 16
Well, you could overload operator <() in your class as that is what is used by default. Or if you want the order reversed use STL's greater and overload operator>().

See here for more info.


Adrian
Thank you very much! I intially was just trying to pass a method and not a type. I now created a new class file and overloaded the operator function. It is now up and running :)
May 30 '07 #5

AdrianH
Expert 100+
P: 1,251
Thank you very much! I intially was just trying to pass a method and not a type. I now created a new class file and overloaded the operator function. It is now up and running :)
Execlent! Release the hounds Smithers!

;)


Adrian
May 30 '07 #6

Post your reply

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