[英]Use of std::greater
I am curious about the use of std::greater . 我很好奇std :: greater的使用。 When used with
sort
, it outputs the numbers in descending order. 与
sort
使用时,它会按降序输出数字。 But when used with priority_queue
, numbers are output in ascending order. 但是当与
priority_queue
使用时,数字按升序输出。 Why so? 为什么这样?
Example: 例:
#include <iostream> // std::cout
#include <functional> // std::greater
#include <algorithm> // std::sort
#include <queue> // std::priority_queue
int main () {
int numbers[]={20,40,50,10,30};
std::priority_queue<int, std::vector<int>, std::greater<int>> pq (numbers, numbers+5);
std::sort(numbers, numbers + 5, std::greater<int>());
while(!pq.empty()){
std:: cout << pq.top() << ' ';
pq.pop();
}
std::cout << '\n';
for (int i=0; i<5; i++)
std::cout << numbers[i] << ' ';
return 0;
}
The output of above code is: 上面代码的输出是:
10 20 30 40 50 50 40 30 20 10
Or similar lines, 或类似的线,
std::priority_queue<int, std::vector<int>, std::greater<int> >
creates a min heap whereas std::priority_queue<int, std::vector<int>, std::less<int> >
creates a max heap. std::priority_queue<int, std::vector<int>, std::greater<int> >
创建一个最小堆,而std::priority_queue<int, std::vector<int>, std::less<int> >
创建最大堆。 Could have been the other way round. 可能反其道而行之。 Why is it so?
为什么会这样?
Citing std::priority_queue
at cppreference [emphasis mine] 在cppreference引用
std::priority_queue
[强调我的]
A priority queue is a container
adaptor
that provides constant time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction.优先级队列是一个容器
adaptor
,它以对数插入和提取为代价,提供最大(默认情况下)元素的常量时间查找。A user-provided
Compare
can be supplied to change the ordering, eg usingstd::greater<T>
would cause the smallest element to appear as thetop()
.可以提供用户提供的
Compare
来更改排序, 例如使用std::greater<T>
将导致最小元素显示为top()
。
So this order is expected, and does not really relate to how std::sort
sorts element based on a supplied binary comparison function. 所以这个顺序是预期的,并没有真正涉及
std::sort
如何根据提供的二进制比较函数std::sort
元素std::sort
排序。
Sorts the elements in the range
[first, last)
in ascending order .按升序对
[first, last)
范围内的元素进行排序。...
...
Parameters
参数
first
,last
- the range of elements to sortfirst
,last
- 要排序的元素范围
policy
- the execution policy to use.policy
- 要使用的执行策略。 See execution policy for details.有关详细信息,请参阅执
comp
- comparison function object (ie an object that satisfies the requirements of Compare) which returnstrue
if thefirst
argument is less than (ie is ordered before) thesecond
.comp
- 比较函数对象(即满足Compare要求的对象) ,如果第first
参数小于(即在之前排序)second
参数,则返回true
。
As std::greater
will return true
if its first argument is greater than its second one, we expect the elements to be sorted in descending order when using std::sort
with std::greater
as function object for performing comparisons. 由于
std::greater
如果第一个参数大于第二个参数将返回true
,我们期望在使用std::sort
和std::greater
作为执行比较的函数对象时,元素按降序std::sort
。
Ie, std::greater
just happens to be the function object used for performing comparisons in these two different contexts of your example. 即,
std::greater
恰好是用于在示例的这两个不同上下文中执行比较的函数对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.