I am not very familiar with deriving from templates, see the following:
class BCQueue_;
template<typename T,
typename Sequence = std::vector<T>,
typename Compare = std::less<typename Sequence::value_type> >
class FixedPriorityQueue : public std::priority_queue<T> {
friend class BCQueue_; // to access private member maxSize_
public:
FixedPriorityQueue(unsigned int maxSize)
: maxSize_(maxSize) {}
// smallest item will be dropped if queue id full
void insertWithOverflow(const T& x) {
// ...
this->push(x);
// ...
}
private:
FixedPriorityQueue() {}
const unsigned int maxSize_;
};
class ScoreLessThan : public std::binary_function<CCandidate *, CCandidate *, bool> {
public:
bool operator()(
const CCandidate * a, const CCandidate * b) const {
std::wcout << a->score << " < " << b->score << std::endl;
return a->score > b->score;
}
};
class BCQueue_ : public FixedPriorityQueue<
CCandidate *, std::vector<CCandidate *>, ScoreLessThan> {
public:
BCQueue_(size_t maxSize)
: FixedPriorityQueue(maxSize) {}
bool willInsert(float score) {
return size() < maxSize_ || top()->score < score;
}
};
BSQueue bc(20);
bs.insertWithOverflow(new CCandidate( ... ));
// ...
In the FixedPriorityQueue I have a method called insertWithOverflow
that inserts elements (if the queue is full) by removing the smallest element.
To keep things a little separated, I derive from FixedPriorityQueue. Basically all works fine, except that the comparator ScoreLessThan
seems to be never called. When I top/pop the elements, they are not in the order (of the score) as I would expect them.
So that might be more a syntax/usage problem, please bear with me, but I could not find any answer to solve the question. I though by defining the template parameters all would work out. What I am missing here?
Thanks for the help!