priority_queue question...

Started by
2 comments, last by Ahl 12 years, 11 months ago
Just a question about some code I was playing around with. Consider the following:

std::priority_queue<MyClass *> PriQue;

MyClass, obviously, being a class object I'm passing by pointer. priority_queue defaults to sorting by the pointer. Is there a way, when passing objects by pointer, into a priority_queue, to get it to sort the objects by some other factor like a member variable? I'm guessing the answer is no as it ignores overridden operators, but ya never know. There might be some trick I'm missing.

Thanks in advance.
Advertisement
You need to specify a custom Compare template parameter (which should be a BinaryPredicate).

See here: http://www.sgi.com/t...rity_queue.html

And see here for an example.

Actually I'll give you a better example:


bool LessThanPointers(MyClass* a, MyClass* b)
{
return ( *a < *b);
// or return ( a->someMember() < b->someMember() )
}

std::priority_queue<MyClass*, std::vector<MyClass*>, LessThanPointers> myPriorityQueue;


Just a question about some code I was playing around with. Consider the following:

std::priority_queue<MyClass *> PriQue;

MyClass, obviously, being a class object I'm passing by pointer. priority_queue defaults to sorting by the pointer. Is there a way, when passing objects by pointer, into a priority_queue, to get it to sort the objects by some other factor like a member variable? I'm guessing the answer is no as it ignores overridden operators, but ya never know. There might be some trick I'm missing.

Thanks in advance.


The full version of the template declaration is:


template < class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;

You just happen to use the default container and default comparison.


By storing pointers the default comparison will be a simple sort of the memory address using the less than operator.

To sort differently you will need to specify your own comparison function. Your custom comparison function can do whatever you want, including looking at member variables or look to external sources.

//Edit: since it is a pointer there is no operator< to override.

[quote name='Ahl' timestamp='1306363075' post='4815802']
Just a question about some code I was playing around with. Consider the following:

std::priority_queue<MyClass *> PriQue;

MyClass, obviously, being a class object I'm passing by pointer. priority_queue defaults to sorting by the pointer. Is there a way, when passing objects by pointer, into a priority_queue, to get it to sort the objects by some other factor like a member variable? I'm guessing the answer is no as it ignores overridden operators, but ya never know. There might be some trick I'm missing.

Thanks in advance.


The full version of the template declaration is:


template < class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;

You just happen to use the default container and default comparison.

To sort differently you will need to either override operator<() or specify your own comparison function. Your custom comparison function can do whatever you want, including looking at member variables or look to external sources.
[/quote]

I've tried overloading the < (less than) operator in the class definition but it's ignored. I'll have to look at the other things you mentioned. I hadn't thought about those. Thanks.

This topic is closed to new replies.

Advertisement