- Nov 29, 2005
- 2,411
- 0
- 0
EDIT: New Question!
Here's the new question:
Is there any way of making the priority function of a PQ modular? That is, can I specify at run-time which comparison function I'd like to use? The first response hinted at that, but it'd be more complicated than simply reversing the list. All the comparisons are simple (they're just integral proprieties).
For example: I might want to sort the node list by shortest latency. But then I might want to sort it by max distance or something. Is there any way of maintaining the same priority queue object but having a switch to pick one?
The main reason is I have a function that returns this priority queue. And obviously queues with different comparisons are completely different object types. So if I have a function return priority queue type A, when I change the comparison function, the return type is now invalid.
Is there any way of getting around this?
Basically, I have a bunch of nodes in a list, and I want to write a function that'll return them sorted in whatever way I'd like, preferably without having to do the sorting myself.
Thanks!
Derp. Wouldn't the following work?
****
Here's the old question
****
So I want to use a priority queue to order up some "node" class objects that I have. These nodes have a couple of properties -- all integral -- that I'd like to be able to order by. Do I have do define different "compare" functions for each of these? i.e.
bool operator() (const node & leftNode, const node & rightNode) const {
if (leftNode->getMyValue() <= rightnode->getMyValue() )
return TRUE
else
return FALSE;
}
For each of the differing myValue properties I might want to use? Or is there an easier way where I can define the integer value in the [priority queue] object definition?
(And if I have to define the operator as above, where do I do that? In the node class?)
Thanks!
Here's the new question:
Is there any way of making the priority function of a PQ modular? That is, can I specify at run-time which comparison function I'd like to use? The first response hinted at that, but it'd be more complicated than simply reversing the list. All the comparisons are simple (they're just integral proprieties).
For example: I might want to sort the node list by shortest latency. But then I might want to sort it by max distance or something. Is there any way of maintaining the same priority queue object but having a switch to pick one?
The main reason is I have a function that returns this priority queue. And obviously queues with different comparisons are completely different object types. So if I have a function return priority queue type A, when I change the comparison function, the return type is now invalid.
Is there any way of getting around this?
Basically, I have a bunch of nodes in a list, and I want to write a function that'll return them sorted in whatever way I'd like, preferably without having to do the sorting myself.
Thanks!
Derp. Wouldn't the following work?
Code:
class myCompare {
int flag;
public:
myCompare (int flagParm = 0) {flag = flagParm;}
bool operator() (node node1, node node2) {
switch (flag) {
case 1:
if (node1->valueA > node2->valueA)
return TRUE;
else
return FALSE;
break;
case 2:
if (node1->valueX > node2->valueY)
return TRUE;
else
return FALSE;
break;
} // switch
}
};
****
Here's the old question
****
So I want to use a priority queue to order up some "node" class objects that I have. These nodes have a couple of properties -- all integral -- that I'd like to be able to order by. Do I have do define different "compare" functions for each of these? i.e.
bool operator() (const node & leftNode, const node & rightNode) const {
if (leftNode->getMyValue() <= rightnode->getMyValue() )
return TRUE
else
return FALSE;
}
For each of the differing myValue properties I might want to use? Or is there an easier way where I can define the integer value in the [priority queue] object definition?
(And if I have to define the operator as above, where do I do that? In the node class?)
Thanks!
Last edited:
