Skip to main content
added 464 characters in body
Source Link
dfrib
  • 73.6k
  • 12
  • 138
  • 201

Citing std::priority_queue at cppreference [emphasis mine]

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.

A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<T> would cause the smallest element to appear as the top().

So this order is expected, and does not really relate to how std::sort sorts element based on a supplied binary comparison function.

Sorts the elements in the range [first, last) in ascending order.

...

Parameters

  • first, last - the range of elements to sort

  • policy - the execution policy to use. See execution policy for details.

  • comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than (i.e. is ordered before) the second.

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.


I.e., std::greater just happens to be the function object used for performing comparisons in these two different contexts of your example.

Citing std::priority_queue at cppreference [emphasis mine]

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.

A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<T> would cause the smallest element to appear as the 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::greater just happens to be the function object used for performing comparisons in these two different contexts of your example.

Citing std::priority_queue at cppreference [emphasis mine]

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.

A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<T> would cause the smallest element to appear as the top().

So this order is expected, and does not really relate to how std::sort sorts element based on a supplied binary comparison function.

Sorts the elements in the range [first, last) in ascending order.

...

Parameters

  • first, last - the range of elements to sort

  • policy - the execution policy to use. See execution policy for details.

  • comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than (i.e. is ordered before) the second.

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.


I.e., std::greater just happens to be the function object used for performing comparisons in these two different contexts of your example.

Source Link
dfrib
  • 73.6k
  • 12
  • 138
  • 201

Citing std::priority_queue at cppreference [emphasis mine]

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.

A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<T> would cause the smallest element to appear as the 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::greater just happens to be the function object used for performing comparisons in these two different contexts of your example.