c++学习笔记--结构体

1.结构体属于用户自己定义的数据类型,允许用户存储不同的数据类型

2.结构体的定义和使用

#include <iostream>
using namespace std;
#include<string>
//1.创建一个学生的数据类型:学生包括(姓名,年龄,分数)
//自定义的数据类型,一些类型集合组成的一个类型
//语法 struct 类型名称{成员列表};
struct Student
{
    string name;
    int age;
    int score;
}s3;//顺便创建结构体变量



int main()
{
    //2.通过学生类型创建具体学生
//2.1 struct Student s1;
    struct Student s1;
    //给s1属性赋值,通过.访问结构体变量中的属性
    s1.name = "张三";
    s1.age = 18;
    s1.score = 100;
    cout << "姓名:" << s1.name << "  年龄:" << s1.age << "  分数:" << s1.score << endl;
 //2.2struct Student s2={....}
    struct Student s2 = { "李四",19,80 };
    cout << "姓名:" << s2.name << "  年龄:" << s2.age << "  分数:" << s2.score << endl;
        system("pause");
 //2.3在创建结构体的时候顺便创建结构体变量
        s3.name = "王五";
        s3.age =20;
        s3.score = 60;
     cout << "姓名:" << s3.name << "  年龄:" << s3.age << "  分数:" << s3.score << endl;
    return 0;
}

3.结构体数组

#include <iostream>
using namespace std;
#include<string>
//创建一个结构体
struct Student
{
    string name;
    int age;
    int score;
};

int main()
{
   //结构体数组
    struct Student arr[3] = { {"张三",20,100},{"李四",19,80},{"王五",18,60} };
    //给结构体元素赋值
    arr[1].name = "赵六";//可以通过这样来修改结构体数字中的值
    for (int i = 0;i<3;i++)
    {
        cout << "姓名:" << arr[i].name << "  年龄:" <<arr[i].age << "  分数:" << arr[i].score << endl;
        
    }
    return 0;
}

4.结构体指针

作用:通过指针访问结构体中的成员

利用->可以通过结构体指针访问结构体属性

#include <iostream>
using namespace std;
#include<string>
//创建一个结构体
struct Student
{
    string name;
    int age;
    int score;
};

int main()
{
   //结构体变量
    struct Student s1 = { "张三",20,100};//struct可以省略
   //通过指针指向结构体变量
    struct Student* p = &s1;//struct可以省略
   //通过指针访问结构体变量的数据  格式p->

    cout <<"姓名: "<<p->name <<"年龄: "<<p->age<<"分数: "<<p->score<< endl;
    system("pause");
    return 0;
}

5.结构体嵌套结构体

作用:结构体中的成员可以是另一个结构体

例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

#include <iostream>
using namespace std;
#include<string>
//学生结构体定义
struct Student
{
    string name;
    int age;
    int score;
};
//教师结构体定义
struct teacher
{
    int id;
    string name;
    int age;
    struct Student stu;//结构体嵌套结构体
};
int main()
{
    //创建结构体变量 ,结构体嵌套结构体
    struct teacher t2;
    t2.id = 1;
    t2.name = "吴柏";
    t2.age = 23;
    t2.stu.age = 18;
    t2.stu.name = "吴三妹";
    t2.stu.score = 100;
    cout << "id=" << t2.id << "  姓名:" << t2.name << "  年龄:" << t2.age <<endl
        << "  学生姓名:" << t2.stu.name << "  学生年龄:" << t2.stu.age << "  学生分数:" << t2.stu.score << endl;


 
    system("pause");
    return 0;
}

 6.结构体做函数的参数

作用:将结构体作为参数向函数中传递

传递方式有两种:值传递、地址传递(注意两者区别)

#include <iostream>
using namespace std;
#include<string>
//学生结构体定义
struct Student
{
    string name;
    int age;
    int score;
};
struct Student s2 = { "李四",19,80 };
//打印学生信息的函数
//1.值传递
void printfstu(struct Student s1)
{
    s1.age = 100;//运行后可以发现main函数中年龄并未发生变化,因为值传递不改变实参
    cout << "子函数1中  姓名:" << s1.name << "  年龄:" << s1.age << "  分数:" << s1.score << endl;
}
//2.地址传递
void  printfstu2(struct Student *p)
{
    p->age = 100;//运行之后可以发现main函数中年龄发生了改变,因为地址传递改变实参
    cout << "子函数2中   姓名:" << p->name << "  年龄:" << p->age << "  分数:" << p->score<< endl;//注意这里是指针访问

}
int main()
{
   //结构体做函数参数
   //将学生传入到一个参数中,打印学生身上的所有信息
   //创建结构体变量
    struct Student s1 = { "张三",20,100 };
    struct Student s2 = { "李四",19,80 };
    printfstu(s1);
    printfstu2(&s2);
   cout << "main函数中打印 姓名:" << s1.name << "  年龄:" << s1.age << "  分数:" << s1.score << endl;
   cout << "main函数中打印 姓名:" << s2.name << "  年龄:" << s2.age << "  分数:" << s2.score << endl;

 
    system("pause");
    return 0;
}

 7.结构体中const使用场景

作用:用const来防止误操作

#include <iostream>
using namespace std;
#include<string>
//const的使用场景
//学生结构体定义
struct Student
{
    string name;
    int age;
    int score;
};
//将函数中的形参改为指针,可以减少内存空间,因为指针在64位系统中只占8个字节,如果用值传递可能会存在信息量很大,占用内存空间过大。
void printfstu(const struct Student *p)//地址传递
{
   // p->age = 100;//加了const是为了避免地址传递时,对实参的数据修改。
    //用地址传递是为了节省代码运行的内存空间,但地址传递是可以影响实参的,所以我们加上const避免实参被修改的错误。
    cout <<  "姓名:" << p->name << "  年龄 : " << p->age << "  分数 : " << p->score << endl;
}

int main()
{
   //创建结构体变量
    struct Student s1 = { "张三",20,100 };
   
    printfstu(&s1);
   
   cout << "main函数中打印 姓名:" << s1.name << "  年龄:" << s1.age << "  分数:" << s1.score << endl;
  
 
    system("pause");
    return 0;
}

8.结构体案例

8.1案例1描述

学校正在做毕设,一个老师带5名学生,总共3名老师;

设计学生和老师的结构体,其中在老师的结构体中,有老师的姓名和一个存放5名学生的数组作为成员。学生的成员有姓名、考试分数、创建数组存放3名老师,通过函数给每个老师及所带的学生赋值最终打印出老师数据以及老师所带学生的数据。

#include <iostream>
using namespace std;
#include<string>

#include<ctime>

//学生结构体定义
struct Student
{
    string name;
   
    int score;
};
//教师结构体定义
struct teacher
{
    string name;
    struct Student arr[5];
};
//创建给老师和学生赋值的函数
void allocateSpace(struct teacher tarr[], int len)
{
    string nameseed = "ABCDE";//这样就不需要我们一个一个的打了
    for (int i = 0; i < len; i++) 
    {
        tarr[i].name = "teacher_";
        tarr[i].name+= nameseed[i];
        for (int j = 0; j < 5; j++)
        {
            
           tarr[i].arr[j].name = "student_";
            tarr[i].arr[j].name += nameseed[j];
            int random = rand() % 61 + 40;//随机数范围
            tarr[i].arr[j].score = random;
           
        }
             
    }
}

void printfinfo(struct teacher tarr[], int len)
{
    for(int i=0;i<len;i++)
    {
        cout << "老师的姓名" << tarr[i].name << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << "学生姓名" << tarr[i].arr[j].name << "学生分数" << tarr[i].arr[j].score << endl;
        }

    }
}
int main()
{
    //随机数种子
    srand((unsigned int)time(NULL));
   //创建3名老师的数组
    struct teacher tarr[3];
    //通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
    int len = sizeof(tarr) / sizeof(tarr[0]);
    allocateSpace( tarr,len);
    //打印所有老师及所带学生的信息
    printfinfo(tarr, len);

    
 
    system("pause");
    return 0;
}

8.2案例2

描述:设计一个英雄的结构体,总共5名英雄,通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序的结果。

#include <iostream>
using namespace std;
#include<string>

#include<ctime>

//英雄结构体定义
struct hero
{
    string name;
    int age;
    string sex;
};

void bubbleSort(struct hero harr[], int len)
{
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = 0; j < len - i - 1; j++)
        {
            if (harr[j].age > harr[j + 1].age)
            {
                struct hero temp;//注意这里的交换代码格式
                temp = harr[j];
                harr[j] = harr[j + 1];
                harr[j + 1] = temp;
            }
        }
    }
}
void printfinfo(struct hero harr[], int len)
{
    for(int i=0;i<len;i++)
    {
        cout << "英雄的姓名:" << harr[i].name 
            << "年龄:"<< harr[i].age
            <<"性别:"<<harr[i].sex<<endl;
        

    }
}
int main()
{
   
   //创建5名英雄的数组
    struct hero harr[5] = { {"刘备",23,"男"}, {"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"},};
    int len = sizeof(harr) / sizeof(harr[0]);
    //调用冒泡函数
    bubbleSort(harr, len);
    printfinfo(harr, len);
    
 
    system("pause");
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值