Shreya Raj
Shreya Raj

Reputation: 21

why is less<T> comparator used for max heap and greater<T> for min heap?

Shouldn't it be the opposite? what do we mean by less and greater in detail when we use them as comparators I don't understand the complete explanation for this, could someone please help me out.

Upvotes: 2

Views: 377

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122133

It is purley a matter of convention to use the comparator to decide whether one element a comes before another element b. Thats why for increasing order a < b or std::less is used and for decreasing order a > b / std::greater.

If the convention was the opposite: Use the comparater to decide which element comes last. Then < would result in decreasing order and > would result in increasing order.

Upvotes: 1

Related Questions