首页 > 其他分享 >sort使用

sort使用

时间:2023-01-15 16:44:25浏览次数:34  
标签:sort node 函数 int 使用 include cmp

用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

标签:sort,node,函数,int,使用,include,cmp
From: https://www.cnblogs.com/cornfield/p/17053705.html

相关文章