C++优先队列自定义排序总结

C++优先队列自定义排序总结一、优先队的使用:二、基本数据类型:priority_queue<int>q;//默认是从大到小priority_queue<int,vector<int>,less<int>>q;//从大到小排序priority_queue<int,vector<int>,greater<int>>q…

大家好,欢迎来到IT知识分享网。

一、优先队的使用:
在这里插入图片描述

二、基本数据类型:

priority_queue<int> q;//默认是从大到小

priority_queue<int, vector<int> ,less<int> >q;//从大到小排序

priority_queue<int, vector<int>, greater<int> >q;//从小到大排序

三、自定义类型:
第1种:

struct Node
{ 
   
	char data;
	int weight;
	Node(char _data, int _weight)
	{ 
   
		data = _data;
		weight = _weight;
	}
};

(1)weight大的优先级高 :

struct cmp1
{ 
   
	bool operator () (const Node &a, const Node &b)		  
	{ 
   
		return a.weight < b.weight;			
	}
};

构造优先队列:

priority_queue< Node, vector<Node>, cmp1> q1;

(2)weight小的优先级高

struct cmp2
{ 
   
	bool operator()(const Node &a, const Node &b)		  
	{ 
   
		return a.weight > b.weight;			
	}
};

构造优先队列:

priority_queue<Node,vector<Node>,cmp2> q2;

第2种
重载 < 运算符
(1)weight大的优先

struct Node2 
{ 
   
	char data;
	int weight;
	Node2(char _data, int _weight)
	{ 
   
		data = _data;
		weight = _weight;
	}
	friend bool operator < (const Node2 &a,const Node2 &b)
	{ 
   
		return a.weight < b.weight; 		
	} 
};

构造优先队列:

priority_queue<Node2> q3;

(2)weight小的优先

struct Node3 
{ 
   
	char data;
	int weight;
	Node3(char _data, int _weight)
	{ 
   
		data = _data;
		weight = _weight;
	}
	friend bool operator < (const Node3 &a,const Node3 &b)
	{ 
   
		return a.weight > b.weight; 
	} 
};

构造优先队列:

priority_queue<Node3> q4;

test代码:

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

struct Node
{ 
   
	char data;
	int weight;
	Node(char _data, int _weight)
	{ 
   
		data = _data;
		weight = _weight;
	}
};
struct cmp1
{ 
   

	bool operator () (const Node &a, const Node &b)		  
	{ 
   
		return a.weight < b.weight;			//大的优先级高 
	}
}; 
struct cmp2
{ 
   
	bool operator()(const Node &a, const Node &b)		  
	{ 
   
		return a.weight > b.weight;			//小的优先级高 
	}
};
bool cmp(const Node &a,const Node &b)
{ 
   
	return a.weight > b.weight;
}
struct Node2 
{ 
   
	char data;
	int weight;
	Node2(char _data, int _weight)
	{ 
   
		data = _data;
		weight = _weight;
	}
	friend bool operator < (const Node2 &a,const Node2 &b)
	{ 
   
		return a.weight < b.weight; 
	} 
};
struct Node3 
{ 
   
	char data;
	int weight;
	Node3(char _data, int _weight)
	{ 
   
		data = _data;
		weight = _weight;
	}
	friend bool operator < (const Node3 &a,const Node3 &b)
	{ 
   
		return a.weight > b.weight; 
	} 
};
int main()
{ 
   
	string values = "ABCDEF";
	vector<int> weights = { 
   1,2,3,4,5,6};
	priority_queue< Node, vector<Node>, cmp1> q1;
	for(int i = 0; i < values.length(); i++)
	{ 
   
		Node newNode(values[i],weights[i]); 
		q1.push(newNode);
	}
	cout<<"优先队列q1的第一个元素是: "<<q1.top().data<<endl;
	
	priority_queue<Node,vector<Node>,cmp2> q2;
	for(int i = 0; i < values.length(); i++)
	{ 
   
		Node newNode(values[i],weights[i]); 
		q2.push(newNode);
	}
	cout<<"优先队列q2的第一个元素是: "<<q2.top().data<<endl;
	
	priority_queue<Node2> q3;
	for(int i = 0; i < values.length(); i++)
	{ 
   
		Node2 newNode(values[i],weights[i]); 
		q3.push(newNode);
	}
	cout<<"优先队列q3的第一个元素是: "<<q3.top().data<<endl;
	
	priority_queue<Node3> q4;
	for(int i = 0; i < values.length(); i++)
	{ 
   
		Node3 newNode(values[i],weights[i]); 
		q4.push(newNode);
	}
	cout<<"优先队列q4的第一个元素是: "<<q4.top().data<<endl;
}

运行截图:
在这里插入图片描述


更新:

priority_queue< pair<double,int>,
			   	vector<pair<double,int> >,
			   	greater<pair<double,int> > > pq;	//first小的优先
priority_queue< pair<double,int>,
			   	vector<pair<double,int> >,
			   	less<pair<double,int> > > pq;	//first大的优先

first小的优先

class cmp
{ 
   
public:
	bool operator()(pair<double,int> x, pair<double,int> y)
	{ 
   
		return x.first > y.first;
	}
};			   	

first大的优先

class cmp
{ 
   
public:
	bool operator()(pair<double,int> x, pair<double,int> y)
	{ 
   
		return x.first < y.first;
	}
};			   	

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/21139.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信