简体   繁体   English

C ++中std :: priority_queue的比较器部分的含义是什么?

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

Priority queue syntax in C++: 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). 如果要声明最大堆,请使用std::less ,否则使用std::greater (最小堆)。 I don't quite get why std::less leads to a max heap, and std::greater min heap? 我不太明白为什么std::less会导致最大堆,而std::greater最小堆会导致最大堆?

A quote from cppreference that explains the concept: 来自cppreference的引言解释了这个概念:

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. 请注意,Compare参数的定义是,如果第一个参数以弱顺序出现在第二个参数之前,则返回true。 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. 也就是说,根据比较所施加的弱排序,队列的前部包含“最后”元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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