- 声明结构体
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);