I\'m still confused about priority queue in STL. Here is the objective I wanna achieve, say: I have a structure called Record, which contains a string word and a int counter. Fo
Your code does work, with two small changes:
Record::operator<()
, since that's needed by the priority queue's default comparator.bool operator<(const Record &) const
(note the extra const
), since the priority queue has to compare using references to const
objects. Alternatively, declare it as a free function, outside the class definition:
bool operator<(const Record &l, const Record &r) {return l.count > r.count;}
or define your own functor, and provide that as the appropriate template argument:
struct CompareRecords
{
bool operator()(const Record &l, const Record &r) {return l.count > r.count;}
};
typedef priority_queue, CompareRecords> RecordQueue;