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;
}
}
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.
Re: Sorting an STL of pointers to comparable objects.
Originally Posted by OrangeKyo
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;
}
}
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
Well you almost had it.
1- Why would you even try to pass a pointer as a const reference? Just pass it by value. That, and passing a pointer by reference is:
Code:
const Leaf*& p1
not
const Leaf&* p1
2- what the heck are you doing with that priority queue of vectors?
just call sort with the compare operator:
Code:
leaves.sort(PairComparator());
3- How can you call sort on leaves? It's a vector! vectors don't have sort. Do it like this:
4- You might be confused at what a priority queue is. First, you can't sort a priority queue. Second, building a priority Queue only takes two arguments: the objects, and the comparer.
does not create a priority queue of vectors. It creates a priority queue with the base container as vectors, and a comparison function of pair comparator.
does not create a priority queue of vectors. It creates a priority queue with the base container as vectors, and a comparison function of pair comparator.
Re: Sorting an STL of pointers to comparable objects.
A priority queue (aka, heap) is the data structure of choice when you're going to be doing a lot of inserts at various times, and you only care about the smallest (largest) element in the data structure at any given time, not in maintaining a full sort at all times. It's likely to be slightly faster than a std::multiset for this use-case on most compilers, and certainly faster than a std::sort() call after every insertion to a std::vector.
Bookmarks