简体   繁体   中英

What is the meaning of the comparator part of the std::priority_queue in C++?

Priority queue syntax in C++:

priority_queue <Type, vector<Type>, ComparisonType > min_heap;

If I want to declare a max heap, I use std::less , otherwise std::greater (min heap). I don't quite get why std::less leads to a max heap, and std::greater min heap?

A quote from cppreference that explains the concept:

Compare - A Compare type providing a strict weak ordering.

Note that the Compare parameter is defined such that it returns true if its first argument comes before its second argument in a weak ordering. But because the priority queue outputs largest elements first, the elements that "come before" are actually output last. That is, the front of the queue contains the "last" element according to the weak ordering imposed by Compare.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM