结构体总结

目录

1.普通结构体

2.定义结构体并同时建立变量

3.匿名结构体

 4.typedef重命名

5.typedef省略结构体名字

6.结构体数组

 7.结构体指针

8.结构体嵌套

 9.结构体链表(头插法)

 10.结构体中的函数指针

 11.结构体的初始化列表

 12.结构的的构造函数

13.结构体重载运算符


1.普通结构体

struct   name(结构体名){

        结构体成员

};

#include 
struct node
{
    int a;
    char *b;
};

int main(int argc, char const *argv[])
{
    struct node n;
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

2.定义结构体并同时建立变量

struct   name(结构体名){

        结构体成员

}实例变量;

#include 
struct node
{
    int a;
    char *b;
}n,nn;

int main(int argc, char const *argv[])
{
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

3.匿名结构体

struct{

        结构体成员

}实例变量;

#include 
struct
{
    int a;
    char *b;
}n,nn;

int main(int argc, char const *argv[])
{
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

 4.typedef重命名

typedef  struct   name1结构体名){

        结构体成员

}name2(重新定义的名字);

name2=struct name1

#include 
typedef struct node
{
    int a;
    char *b;
}node;

int main(int argc, char const *argv[])
{
    node n;
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

5.typedef省略结构体名字

typedef  struct {

        结构体成员

}name2(重新定义的名字);

#include 
typedef struct
{
    int a;
    char *b;
}node;

int main(int argc, char const *argv[])
{
    node n;
    n.b="abcdefg";
    n.a=100;
    printf("%s\n%d\n",n.b,n.a);
    return 0;
}

6.结构体数组

和创建普通数组一样创建结构体数组即可

#include 
typedef struct node
{
    int a;
    char *b;
}node;

int main(int argc, char const *argv[])
{
    node n[10];
    for(int i=0;i<10;i++){
        n[i].a=i*i;
        n[i].b="zbcdef";
    }
     for(int i=0;i<10;i++){
        printf("%d\t%s\n",n[i].a,n[i].b);
    }
    return 0;
}

结构体总结_第1张图片

 7.结构体指针

extern void *malloc(unsigned int num_bytes);

首先定义一个结构体指针,但此时只是分配了一个地址,结构体成员变量并没有分配空间,因此使用malloc函数动态分配地址, 返回值是一个void*,因此要强转成你要的类型指针。

结构体成员变量赋值:

假如n是一个结构体指针,*n就是这个实际结构体,  (*n).成员名   就可以给结构体成员赋值或者更改,此外C语言为了简便,单独规定了结构体指针的定义方式  n->成员名。两种都是可以的;

#include 
typedef struct
{
    int a;
    char*  b;
}node;

int main(int argc, char const *argv[])
{
    node *n=(node*)malloc(sizeof(node));
    (*n).b="abcdefg";
    (*n).a=100;
    printf("%s\n%d\n",(*n).b,(*n).a);
    return 0;
}

#include 
typedef struct
{
    int a;
    char*  b;
}node;

int main(int argc, char const *argv[])
{
    node *n=(node*)malloc(sizeof(node));
    n->b="abcdefg";
    n->a=100;
    printf("%s\n%d\n",n->b,n->a);
    return 0;
}

8.结构体嵌套

结构体的成员变量当然可以包含结构体了

#include 
typedef struct student
{
   char* name;
   int age;
   int chengji;
}student;

typedef struct school_class
{
    student std[3];
    char* t_name;
    int t_age;
    
}school_class;

int main(int argc, char const *argv[])
{
    school_class *n=(school_class*)malloc(sizeof(school_class));
    n->t_name="曹老师";
    n->t_age=21;
    n->std[0].name="小明";
    n->std[0].age=18;
    n->std[0].chengji=500;
    n->std[1].name="小张";
    n->std[1].age=17;
    n->std[1].chengji=600;
    n->std[2].name="小红";
    n->std[2].age=18;
    n->std[2].chengji=700;
    printf("老师信息:\n%s\t%d\n",n->t_name,n->t_age);
    for(int i=0;i<3;i++){
        printf("学生%d信息:\n",i);
        printf("%s\t%d\t%d\n",n->std[i].name,n->std[i].age,n->std[i].chengji);
    }
    return 0;
}

结构体总结_第2张图片

 9.结构体链表(头插法)

链表就是结构体的指针指向下一个结构体,其存储不连续,是分布式的,因此读取速度相对数组来说慢的多。定义一个结构体,其结构体成员变量中包括本身的一个结构体指针,因此可以给该结构体变量赋值,赋的值又是一个结构体指针,里面又有一个结构体指针类型的结构体成员,以此类推产生链表。C++的STL库有list库,使用十分方便;

#include"stdlib.h"
#include"stdio.h"
static int wei=1;
typedef struct Node 
{
	char data[15]; 
	struct Node* next;
}node;
node* head;
void Insert()
{
	node* p=(node*)malloc(sizeof(node));
	char buff[10];
	sprintf(buff,"我是第%d个元素!",wei);
	int i=0;
    while(buff[i]!='\0'){
    	p->data[i]=buff[i];
    	i++;
	}
	p->data[i]='\0';
    p->next=head;
    wei++;
    head=p;
 }
 
void Print()
{
	node* p=head;
	while(p!=NULL)
	{
		printf("%s\n",p->data);
		p=p->next;
	}
}//自定义打印函数(Print)
 
int main()
{
	head=NULL;
	for(int i=0;i<5;i++)
	{
	    Insert();
	}
	Print();
}

 结构体总结_第3张图片

 10.结构体中的函数指针

#include 
typedef struct Node{
	int x;
	int y;
	int (*cheng)(int x,int y);
}node;

int cheng_1(int x,int y){
	return x*y;
}
int main(){
	node m;
	m.x=10;
	m.y=9;
	m.cheng=cheng_1;
	printf("%d\n",m.cheng(m.x,m.y));
	return 0;
}

 结构体总结_第4张图片

 11.结构体的初始化列表

#include 
using namespace std;
struct node{
	int age;
	string name;
	int weight;
	int height;
	node(string name_1="曹XX",int age_1=18,int weight_1=120,int height_1=180){
		name=name_1;
		age=age_1;
		weight=weight_1;
		height=height_1; 
	}
};
int main(){
	node cao("曹仙人",22,123,185);
	cout<

结构体总结_第5张图片

 12.结构的的构造函数

#include 
using namespace std;
struct node{
	string name;
	int age;
	int weight;
	int height;
	node():name("曹仙人"),age(22),weight(120),height(190){
		cout<<"构造函数"<

 结构体总结_第6张图片

13.结构体重载运算符

#include 
#include 
using namespace std;
struct node{
	int x;
	int y;
	bool operator<(const node &rhs)const {
		if(x==rhs.x){
			return y>rhs.y;
		}else{
			return x>rhs.x;
		}
	}
};
int main(){
	node a[10];
	for(int i=0,j=10;i<10;i++,j--){
		a[i].x=i;
		a[i].y=j;
	}
	cout<<"排序前"<

 结构体总结_第7张图片

你可能感兴趣的:(c++,算法,开发语言,学习)