C++学习之旅(黑马程序员版本)-----结构体

结构体

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
自定义数据类型,一些类型的集合

#include<iostream>
#include<string>
using namespace std;
struct student
{
    string name;
    int age;
    int score;
}s3;



int main()
{
    //给结构体变量赋值的三个方法
    //sturct可以不写
    struct student s1;
    s1.name = "ZHANGSAN";
    s1.age =18;
    s1.score = 100;
    cout<<"name:"<<s1.name<<" age: "<<s1.age<<" score:"<<s1.score<<endl;

    struct student s2 = {"lisi",18,80};
    cout<<"name:"<<s2.name<<" age: "<<s2.age<<" score:"<<s2.score<<endl;

    s3.name = "wangwu";
    s3.age = 20;
    s3.score = 90;
    cout<<"name:"<<s3.name<<" age: "<<s3.age<<" score:"<<s3.score<<endl;
}

结构体数组

#include<iostream>
#include<string>
using namespace std;
struct student
{
    string name;
    int age;
    int score;
}s3;

int main()
{
    struct student arr[3] = 
    {
        {"zhangsan",18,100},
        {"lisi",19,90},
        {"wangwu",20,80},
    };
    cout<<"stu1"<<arr[0].name<<arr[0].age<<arr[0].score<<endl;
    cout<<"stu2"<<arr[1].name<<arr[1].age<<arr[1].score<<endl;
    cout<<"stu3"<<arr[2].name<<arr[2].age<<arr[2].score<<endl;
}

结构体指针

结构体指针可以访问结构体中的成员
需要利用->

#include<iostream>
#include<string>
using namespace std;
struct student
{
    string name;
    int age;
    int score;
}s3;

int main()
{
    struct student stu1 = {"zhangsan",18,100};
    struct student *p = &stu1;
    cout<<"name: "<<p->name<<" age: "<<p->age<<"score:"<<p->score<<endl;
}

结构体嵌套结构体

在结构体可以嵌套结构体,用来解决实际问题。

#include<iostream>
#include<string>
using namespace std;
struct student
{
    string name;
    int age;
    int score;
};
struct teacher
{
    int id;
    string name;
    struct student stu;
    
};
int main()
{
    struct teacher t1;
    t1.stu.age = 19;
    cout<<"t1.stu.age:"<<t1.stu.age<<endl;
}

结构体做函数参数

传递方式:
值传递:形参不会改变实参
地址传递:实参会改变

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

struct student
{
    string name;
    int age;
    int score;
};
struct teacher
{
    int id;
    string name;
    struct student stu;
    
};
void ptintStudent01(struct student stu);
void ptintStudent02(struct student *p);
void ptintStudent01(struct student stu)//值传递
{
    stu.age = 100;
    cout<<stu.age<<stu.name<<stu.score<<endl;
}
void ptintStudent02(struct student *p)//地址传递
{
    p->age = 200;
    cout<<p->name<<p->age<<p->score<<endl;
}
int main()
{
    struct student stu1={"zhangsan",18,100};
    ptintStudent01(stu1);
    cout<<stu1.age<<endl;
    ptintStudent02(&stu1);
    cout<<stu1.age<<endl;
}

可以发现,值传递中,真正的stu1.age并没有发生改变,但地址传递中,stu1.age变成了200。这就是因为值传递:形参不会改变实参,地址传递:实参会改变!
在这里插入图片描述
–如果不想修改主函数的数据,使用值传递,反之如果需要修改主函数的数据,使用地址传递。–

结构体中const使用场景

作用:使用const防止误操作

#include<iostream>
#include<string>

using namespace std;   
struct student
{
    string name;
    const int age;
    int score;
};
void printStudent(struct student *p);

void printStudent(struct student *p)
{
    cout<<p->age<<p->name<<p->score<<endl;
}

int main()
{
    struct student s = {"zhangsan",15,70};
    printStudent(&s);
}      

如果在打印函数里添加p->age = 10;会出现报错

结构体案例1

案例描述:学校正在做毕设项目,每个老师带五个学生,需求如下:
设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名,考试分数,创建数组存放三名老师,通过函数给每个老师所带的学生赋值,并最终打印出老师数据和老师所带学生的数据:

#include<iostream>
#include<string>
using namespace std;   
struct student
{
    string name;
    int score;
};
struct teacher
{
    string name;
    struct student stu[5];
};
void Print(struct teacher teac[3]);
string t[3] = {"teacher1","teacher2","teacher3"};
string s[3][5] = {"student1","student2","student3","student4","student5","student6","student7","student8","student9","student10","student11","student12","student13","student14","student15"};

void Print(struct teacher teac[3])
{
    for (int i = 0; i < 3; i++)
    {
        
        for (int j = 0; j < 5; j++)
        {
            teac[i].name = t[i];
            teac[i].stu[j].name = s[i][j];
            teac[i].stu[j].score = 100;
            cout<<"学生"<<j+1<<"的名字是"<<teac[i].stu[j].name<<endl;
            cout<<"学生"<<j+1<<"的分数是"<<teac[i].stu[j].score<<endl;
        }
     cout<<"老师"<<i+1<<"的名字是"<<teac[i].name<<endl;   
    }

}

int main()
{
    struct teacher teac[3];
    int a[3];
    Print(teac);
}

结构体案例2

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
struct hero
{
    string name;
    int age;
    string gender;
};

void bubbsort(struct hero heros[5])
{
    struct hero tem;
    for (int i = 0; i < 5; i++)
    {
        for (int j = i + 1; j < 5; j++)
        {
            if (heros[i].age < heros[j].age)
            {
                tem = heros[i];
                heros[i] = heros[j];
                heros[j] = tem;
            }
        }
    }
    for (int i = 0; i < 5; i++)
    {
        cout<<heros[i].name<<heros[i].age<<heros[i].gender<<endl;
    }
    
}
int main()
{
    struct hero heros[5] = {
        {"liubei", 23, "nan"},
        {"guanyu", 22, "nan"},
        {"zhangfei", 20, "nan"},
        {"zhaoyu", 21, "nan"},
        {"diaochan", 19, "nv"},
    };
    bubbsort(heros);
}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值