结构体排序 + 优先队列排序(priority_queue)

本文详细介绍了如何使用C++的sort函数以及结构体内置排序函数重写比较规则,包括通过cmp函数和自定义运算符实现从大到小的结构体排序,以及在优先队列中应用这些排序方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结构体排序的两种实现方法

1.使用sort函数,重写排序规则。

#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

struct node{
	int x;
	int y;
}a[10];

bool cmp(node a, node b)
{
	return a.x > b.x; //从大到小排列
//  return a.x < b.x;  从小到大排列 
}

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++) 
	 cin >> a[i].x >> a[i].y;
	
    sort(a,a+n,cmp);
    
    for(int i = 0; i < n; i ++) 
     cout << a[i].x << ' ' << a[i].y << endl;
	 
	return 0;
} 

结果演示

2.在结构体中自定义排序,重小于号

#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

struct node{
	int x;
	int y;
	bool operator<(const node &a) const{
		return x < a.x; //从大到小排序 
	                   //  x > a.x;  //从小到大排序 
	}
	//这里的排序是根据每个结构体内部的x进行排序 
}a[10];

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++) 
	 cin >> a[i].x >> a[i].y;
	 

    sort(a,a+n);

    for(int i = 0; i < n; i ++) 
     cout << a[i].x << ' ' << a[i].y << endl;
	 
	return 0;
} 

其实这两种排序都差不多,我推荐大家两种都要学会。

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int main(){
	priority_queue<int> q;//默认是大根堆 
	q.push(1);
	q.push(2);
	q.push(3); 
	cout << q.top() << endl;
	
	        //元素类型、存储元素的容器、排序方式
			//如果是int,char,string,double,long long 等,可以直接使用 
			//如果是结构体,自定义cmp,或者内部实现结构体重载
			// 
	priority_queue<int,vector<int>,less<int> > q2;
	q2.push(2);
	q2.push(3);
	q2.push(4);
	cout << q2.top() << endl; 
	return 0;
} 
/* 内部实现是一个堆 
  q.push(1);加入一个数 
  q.pop();弹出一个数 
  q.top()最大或最小的一个数
  q.size()长度 
*/

上面所描述的是默认的变量类型。然而对于自定义的结构体类型需要采用另外一种方法。

#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

struct node{
	int x;
	int y;
	friend operator <(node a, node b) //第一种自定义排序 
	{
		return a.x < b.x; //从大到小开始排序,因为内部实现是堆,相当于两个孩子比根小
	}
}; 

int main(){
 	priority_queue<node> q;

 	q.push({1,2});
 	q.push({2,3});
 	q.push({3,4});

 	cout << q.top().x << ' ' << q.top().y << endl; 
} 

如果想要和sort(a,a+1,cmp)这样的话,这样一种方法 

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
	int x;
	int y;
};
struct cmp{
    //通常这里尽量加const和&这两个,当数据量比较庞大时,提高效率 
	bool operator () (const node &a, const node &b) 
	{
		return a.x < b.x; //从大到小排序 
	}
}; 
int main(){
//我个人认为这里是和sort(a,a+1,cmp)类似, 
   priority_queue<node,vector<node>, cmp> q;

   q.push({1,2});
   q.push({2,3});
   q.push({3,5});

   cout << q.top().x << ' ' << q.top().y << endl;
   //输出结果是3 5 
} 

优先队列priority_queue)是一种数据结构,它可以按照一定的优先级对元素进行排序和访问。在C++中,通过定义一个结构体,并在结构体中重载小于运算符(operator<),可以实现对结构体排序。 引用中的代码演示了如何使用自定义的结构体进行排序。在这个例子中,定义了一个结构体node,其中包含x和y两个成员变量。通过重载结构体中的小于运算符,可以根据x的大小来进行排序。 引用中的代码演示了如何在优先队列中使用自定义的结构体进行排序。在这个例子中,定义了一个结构体node,并在结构体中重载了小于运算符。通过定义一个优先队列priority_queue<node>,并将结构体放入队列中,可以实现对结构体的自动排序。 所以,如果想要使用优先队列结构体进行排序,可以通过定义一个结构体,并在结构体中重载小于运算符,然后将结构体放入优先队列中即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++利用小根堆实现霍夫曼树](https://download.youkuaiyun.com/download/woaitianbin/86340838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [结构体排序 + 优先队列排序priority_queue)](https://blog.youkuaiyun.com/ssigin/article/details/124689796)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值