简体   繁体   English

使用std :: greater

[英]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 using std::greater<T> would cause the smallest element to appear as the top() . 可以提供用户提供的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 sort firstlast - 要排序的元素范围

  • 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 returns true if the first argument is less than (ie is ordered before) the second . 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::sortstd::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.

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