sort使用

用sort对结构体进行排序步骤如下

1.先写c++头文件

# include<iostream>
# include<algorithm> //这个是sort的头文件 
using namespace std; 

2.定义一个结构体

eg:
struct node{
int x;
int y;

}a[105];

3.写定义函数 (以下为从小到大排列用<,若从大到小用>) 

bool cmp(node a,node b) //函数名任意取,该函数为bool形 
{

  return a.x<b.x;//由小到大

}

4.最后调用

sort(a,a+n,cmp); //n是定义结构体 的长度,cmp为比较函数

摘自:https://blog.csdn.net/luojiushenzi/article/details/80070328

posted @ 2023-01-15 16:34  椿の花少年  阅读(52)  评论(0)    收藏  举报