-
August 11th, 2009, 12:37 AM
#1
Sorting an STL of pointers to comparable objects.
Hi, I have a class called Leaf with a member variable int frequency. I want to load pointers of this class into a priority_queue or a sortable vector so that it sorts based on frequency.
I've tried overloading the < operator and doing this:
vector<Leaf> leaves;
//load the leaves.
leaves.sort();
which works. but what i want is this:
vector<Leaf *> leaves; //Note the pointer type
//load the leaves.
leaves.sort();
I've also tried writing a comparator class and putting that in as an argument to the priority_queue:
class PairComparator {
bool operator()(const Leaf& *p1, const Leaf& *p2) const { //compiler doesn't like this line
return p1->frequency > p2->frequency;
}
}
priority_queue<Leaf*,vector<Leaf*>, PairComparator> leaves;
But that doesn't work. Is there any way to this? Since each Leaf may have pointers to other leaf objects, I MUST use and store the objects as pointers. But as i construct the tree, I must sort them at each iteration. I've searched through the forums quite a bit, and found solutions to sort arrays of pointers, or a priority q of objects with member variables, but not a priority_queue of pointers to objects with member variables.
-OrangeKyo
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|