On Apr 9, 4:46 pm, acehr...@gmail.com wrote:
> On Apr 9, 3:11 pm,KelvinMoss
>
> > Hi all,
>
> > By defaultPriorityQueues use the less<> function object. This causes
> > the highest element to be retrieved first. To get the least element
> > one should use the greater<> function object. Can someone explain how
> > this works?
>
> You need to provide the comparison object type as a template
> parameter. This is easy though, because the comparison type is the
> third template parameter which necessitates to also specify the
> implementation type even though one may not know or do not care. :)
>
> vector should work for apriorityqueueimplementation that uses the
> heap data structure (g++'s implementation uses vector anyway):
>
> #include
> #include
> #include
> #include
>
> typedef std::priority_queue
> std::greater
> int main()
> {
> MyQueue q;
> q.push(2);
> q.push(1);
>
> assert(q.top() == 1);
>
> }
>
> Ali
Perhaps I was not clear in my question. I meant to know that why does
the greatest element get picked with the less <> function object?
Won't it have been more intuitive to return the least element with
less<> function object? Or may be I am missing something?
Thanks ..