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: -
-
-
bool itemCompare(const item& a, const item& b) {
-
return a.number> b.number;
-
}
-
-
void function(){
-
.
-
.
-
.
-
priority_queue<item, vector<item>, boundCompare> itemQueue;
-
.
-
.
-
}
Unfortunatly I get the error of:
bestKS.cpp:56: error: expected a type, got itemCompare
After doing some research I found this: - #include "event.h"
-
#include <queue>
-
using std::priority_queue;
-
-
class greater {
-
public: int operator() (Event *x, Event *y) { return *x > *y; }
-
};
-
priority_queue< Event *, vector<Event *>, greater > _event_queue;
-
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!
5 1668
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.
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.
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
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 :)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: aaronwmail-usenet |
last post by:
I've been wondering about benchmarks recently.
What is a fair benchmark?
How should benchmarks be vetted or judged?
I decided to see what you folks thought, so for discussion I
compared two...
|
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...
|
by: Der Andere |
last post by:
Are priority queues implemented in the STL in Visual Studio 6? If no, which
is the easiest way to simulate them? Multisets?
BTW: The container contains pointers to instances of a class. The...
|
by: Der Andere |
last post by:
I posted this reaction in a thread quite far below so I fear people won't
read it. I got stuck now for quite a while and just don't know what to try
else.
Thanks,
Matthias
> > Are priority...
|
by: Ice |
last post by:
Hi there,
I'm not sure if this is the right group for this- If it isn't, could
anyone point me in the right direction?
For our data structures exam, we are usually asked to implement the...
|
by: Jim Strathmeyer |
last post by:
So, I've been looking for a wrapper class for STL priority queues, and
haven't been able to find one. (Websearching for info about the STL sure
shows how much it's changed over the past few years.)...
|
by: saleem |
last post by:
Dear friends,
I am working on the problem which deals with the data management
of requests and how should we match the responses for the requests to
compare the correct Response.
my problem...
|
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...
|
by: vidishasharma |
last post by:
Can somebody suggest good URL which contains code for merging of 2 or
more priority queues in c#.
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |