结构体及结构体排序

  • 声明结构体
struct example{
	int a;
	int b[10];
	struct example *next; 
}exampl1,example2[10];

struct example就像int、char、float...一样

后面跟着的example1,example2是声明的结构体变量,就相当于int a、char a...里面的a

结构体还有另外一种声明方式

typedef struct example
{
    int a;
    char b;
    double c;
    struct example *next;
} MyStruct,*StructP;

只不过这里面MyStruct相当于int、char...了

  • 指向结构体的指针

struct example *p1;

MyStruct *p2;

StructP p3; 

  • 访问结构体内的变量

example1.a;

p2->a;【必须使用 -> 运算符访问指针型结构体变量的变量成员】

  • 结构体排序

用c++的sort函数

引入头文件

#include<algorithm>

 写cmp函数

struct S{
    int a;
    char b;
}p[10000];

bool cmp(const Student& a, const Student& b)
{
    if (a.a != b.a)
     { 
        return a.a<b.a;
    }
    else if (a.b != b.b)
           return a.b < b.b;
}

调用sort函数

sort(p,p+100,cmp);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值