The difference between the C++ sort() function and the comparison function in the priority_queue container

  • Ordinary queue is a first-in-first-out data structure, elements are appended at the end of the queue and deleted from the head of the queue.
  • Elements in priority_queue are given priority. At the time of creation, according to the priority, it is automatically arranged from large to small or small to large (large top pile or small top pile). You can find the maximum or minimum value in a queue with O(log n) efficiency;

Although the third parameter of both is less by default , the difference between the parameter comparison functions of the two is opposite :

Small details need to pass objects in sort, get less(), and need to pass type less in priority_queue;

less situation

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int main()
{
    
    
    vector<int>arr2 = {
    
    3,2,1};
    sort(arr2.begin(),arr2.end(),less<int>());
    //输出sort()之后的vector
    for (int i = 0; i < 3; i++) {
    
    
        cout << arr2[i] << ' ';
    }
    cout << endl;
    priority_queue<int,vector<int>,less<int>>arr;
    arr.push(3);
    arr.push(2);
    arr.push(1);
    //输出priority_queue
    while (!arr.empty()) {
    
    
        cout<<arr.top()<<' ';
        arr.pop();
    }
    cout << endl;

	return 0;
}

The running result
insert image description here
is then:

  • sort() is sorted from small to large, that is, less is in ascending order;
  • priority_queue is a big top heap, after the output is from large to small, that is, in descending order;

greater situation

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int main()
{
    
    
    vector<int>arr2 = {
    
    3,2,1};
    sort(arr2.begin(),arr2.end(),greater<int>());
    //输出sort()之后的vector
    for (int i = 0; i < 3; i++) {
    
    
        cout << arr2[i] << ' ';
    }
    cout << endl;
    priority_queue<int,vector<int>, greater<int>>arr;
    arr.push(3);
    arr.push(2);
    arr.push(1);
    //输出priority_queue
    while (!arr.empty()) {
    
    
        cout<<arr.top()<<' ';
        arr.pop();
    }
    cout << endl;

	return 0;
}

operation result:
insert image description here

Then:
sort() is sorted from big to small, that is, greater is in descending order;
priority_queue is a small top heap, and after output, it is from small to large, that is, in ascending order;

Custom comparison function case

insert image description here

struct cmp1 //等价于less的内部构造,效果和上面分析的less情况一样
{
    
    
	opeartor()(data x1,data x2){
    
    
		 return x1<x2;
	}
};

struct cmp2 //等价于less的内部构造,效果和上面分析的greater情况一样
{
    
    
	opeartor()(data x1,data x2){
    
    
		 return x1>x2;
	}
};

Summarize

  • When the comparison function is x1.data<x2.data: equivalent to less
  1. sort() will eventually process the sequence into ascending order
  2. priority_queue will be processed into a large root heap –> the traversal output is a descending structure;
  • When the comparison function is x1.data>x2.data: equivalent to greater
  1. sort() will eventually process the sequence into descending order
  2. priority_queue will be processed into a small root heap –> the traversal output is an ascending structure;

They are opposite

Guess you like

Origin blog.csdn.net/wtl666_6/article/details/129215981