结构体总结(二)

简介: 结构体总结

8.结构体嵌套

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

#include <stdio.h>
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;
}

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();
}

10.结构体中的函数指针

#include <stdio.h>
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;
}

 

11.结构体的初始化列表

#include <iostream>
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<<cao.name<<' '<<cao.age<<' '<<cao.height<<' '<<cao.weight<<endl; 
  return 0;
}

12.结构的的构造函数

#include <iostream>
using namespace std;
struct node{
  string name;
  int age;
  int weight;
  int height;
  node():name("曹仙人"),age(22),weight(120),height(190){
    cout<<"构造函数"<<endl; 
  } 
  ~node(){
    cout<<"析构函数"<<endl; 
  }
};
int main(){
  node t;
  cout<<t.name<<' '<<t.age<<' '<<t.weight<<' '<<t.height<<endl;
  return 0;
}

13.结构体重载运算符

#include <iostream>
#include <algorithm>
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<<"排序前"<<endl; 
  cout<<"----------------------"<<endl;
  for(int i=0;i<10;i++){
    cout<<"x:"<<a[i].x<<' '<<"y:"<<a[i].y<<endl;
  }
  sort(a,a+10);
  cout<<"排序后"<<endl; 
  cout<<"----------------------"<<endl;
  for(int i=0;i<10;i++){
    cout<<"x:"<<a[i].x<<' '<<"y:"<<a[i].y<<endl;
  }
  return 0;
} 

相关文章
|
7月前
【结构体】
【结构体】
18 0
|
2月前
|
编译器 Linux C语言
结构体(详解)
结构体(详解)
27 1
|
2月前
|
存储 安全 编译器
一篇文章介绍结构体
一篇文章介绍结构体
19 1
|
3月前
|
存储 C语言
结构体
【2月更文挑战第8天】结构体。
19 5
|
5月前
|
C语言
使用结构体
C 语言实例 - 使用结构体。
104 4
|
6月前
|
机器学习/深度学习 存储 编译器
Day_16 结构体
Day_16 结构体
|
9月前
|
编译器 C++
|
10月前
|
存储 编译器 C语言
C 中的结构体
C 中的结构体
|
11月前
|
C语言