2022.9.24从零开始学习c++的第六天。

结构体指针

#include<iostream>
using namespace std;

struct student
{
    string name;
    int age;
    int score;
};

struct teacher
{
    int id;
    string name;
    int age;
    struct student stu;
};

void printstudent01(struct student s)
{
    s.age = 100;
    cout << s.name << s.age << s.score << endl;
};

//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本
void printstudent02(struct student* p)
//void printstudent02(const student* p)
{
    p->age = 200;//const student* p  一旦有修改操作就会报错
    cout << p->name << p->age << p->score << endl;
};

int main66()
{   //结构体指针
    //通过指针访问结构体中的成员
    //利用->可以通过结构体指针访问结构体书属性
    struct student s = { "张三",18,100 };
    struct student* p = &s;
    cout << p->name << p->age << p->score << endl;

    //结构体嵌套结构体
    //结构体中的成员可以是另外一个结构体
    teacher t;
    t.id = 10000;
    t.name = "老万";
    t.age = 50;
    t.stu.name = "小王";
    t.stu.age = 20;
    t.stu.score = 80;
    cout << t.name << t.age << t.id << endl;
    cout << t.stu.name << t.stu.age << t.stu.score << endl;
    
    //结构体做函数参数
    //将结构体作为参数向函数中传递
    //传递方式:值传递,地址传递
    printstudent01(s);
    printstudent02(&s);
    cout << p->name << p->age << p->score << endl;

    //结构体中的const
    //用const来防止误操作

    system("pause");


    return 0;

}

结构体案例

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

struct teacher
{
    string name;
    struct student sarry[5]; 
};


void allocatespace(struct teacher tarry[], int len)
{
    string nameseed = "abcde";
    for (int i = 0; i < len; i++)
    {
        tarry[i].name = "teacher_";
        tarry[i].name += nameseed[i];
        for (int j = 0; j < 5; j++)
        {
            tarry[i].sarry[j].name = "student_" ;
            tarry[i].sarry[j].name+= nameseed[j];
            tarry[i].sarry[j].score = 60;
        }
    }
}

void printinfo(struct teacher tarry[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << tarry[i].name << endl;
        for (int j=0; j < 5; j++)
        {
            cout << tarry[i].sarry[j].name << tarry[i].sarry[j].score<<endl;
            
        }
    }
}
int main()
{
    //3个老师,每个老师带有五个学生
    struct teacher tarry[3];
    int len = sizeof(tarry) / sizeof(tarry[0]);
    allocatespace(tarry, len);
    printinfo(tarry, len);

    system("pause");

    return 0;
}

#include<iostream>
using namespace std;

struct hero
{
    string name;
    int age;
    string sex;
};

void bubblesort(struct hero * heroarry, int len)
{
    for (int i=1; i < len; i++)
    {
        for (int j = 0; j < len - i; j++)
        {
            if (heroarry[j].age > heroarry[j + 1].age)
            {
                 hero temp = heroarry[j];
                 heroarry[j]= heroarry[j+1];
                 heroarry[j + 1] = temp;
            }
        }
    }
}

void printhore(struct hero * heroarry, int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << heroarry[i].name << heroarry[i].age << heroarry[i].sex << endl;
    }
}
int main71()
{
    struct hero heroarry[5] =
    {
    
        {"刘备",25,"男"},
    
        {"关羽",22,"男"},
    
        {"张飞",20,"男"},
    
        {"赵云",21,"男"},
    
        {"貂蝉",19,"女"}
    };

    int len = sizeof(heroarry) / sizeof(heroarry[0]);
    for (int i = 0; i < len; i++)
    {
        cout << heroarry[i].name << heroarry[i].age << heroarry[i].sex << endl;
    }
    bubblesort(heroarry, len); 
    printhore(heroarry, len);
    system("pause");

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值