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

17 篇文章 0 订阅
13 篇文章 1 订阅

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

二、基本数据类型:

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;
	}
};			   	
  • 31
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值