The reason of using `std::greater` for creating min heap via `priority_queue`

前端 未结 3 1813
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 11:09

I am wondering why for creating a min heap using the priority_queue, the std::greater should be used?

std::priority_queue

        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 11:58

    See http://en.cppreference.com/w/cpp/container/priority_queue. A priority_queue is designed to put the largest value at the top. This happens if you use the default std::less comparator. So if you want the reverse behavior, you need to use the reverse comparator, std::greater.

提交回复
热议问题