C++基础之结构体浅析

一 . 结构体的基本概念

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



二 . 结构体定义和使用

语法 : struct 结构体名 { 结构体成员列表 };

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

//结构体定义
struct Student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
}stu3; //结构体变量创建方式3


int main() {

    //结构体变量创建方式1
    struct Student stu1; //struct 关键字可以省略
    stu1.name = "张三";
    stu1.age = 18;
    stu1.score = 100;
    cout << "姓名:" << stu1.name << " 年龄:" << stu1.age  << " 分数:" << stu1.score << endl;

    //结构体变量创建方式2
    Student stu2 = { "李四",19,60 };
    cout << "姓名:" << stu2.name << " 年龄:" << stu2.age  << " 分数:" << stu2.score << endl;

    stu3.name = "王五";
    stu3.age = 18;
    stu3.score = 80;
    cout << "姓名:" << stu3.name << " 年龄:" << stu3.age  << " 分数:" << stu3.score << endl;

    return 0;
}


三 . 结构体数组

语法 : struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }

#include <iostream>
#include <string>
using namespace std;
//结构体定义
struct Student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};

int main() {

    //结构体数组
    Student arr[3]=
    {
        {"张三",18,80 },
        {"李四",19,60 },
        {"王五",20,70 }
    };

    for (int i = 0; i < 3; i++)
    {
        cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << endl;
    }

    return 0;
}


四 . 结构体指针

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

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

//结构体定义
struct Student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};


int main() {

    Student stu = { "张三",18,100, };

    Student * p = &stu;

    p->score = 80; //指针通过 -> 操作符可以访问成员

    cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;

    return 0;
}


五 . 结构体嵌套结构体

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

//学生结构体定义
struct Student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};

//教师结构体定义
struct Teacher
{
    //成员列表
    int id; //职工编号
    string name;  //教师姓名
    int age;   //教师年龄
    struct Student stu; //子结构体 学生   struct可省略
};


int main() {

    struct Teacher t1;
    t1.id = 10000;
    t1.name = "老王";
    t1.age = 40;

    t1.stu.name = "张三";
    t1.stu.age = 18;
    t1.stu.score = 100;

    cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;

    cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;

    return 0;
}


六 . 结构体做函数参数

传递方式有两种:

  • 值传递(只能改变形参的值)
  • 地址传递(可以改变实参的值)
#include <iostream>
#include <string>
using namespace std;

//学生结构体定义
struct Student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};

//值传递
void printStudent(Student stu )
{
    stu.age = 28;
    cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age  << " 分数:" << stu.score << endl;
}

//地址传递
void printStudent2(Student *stu)
{
    stu->age = 28;
    cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age  << " 分数:" << stu->score << endl;
}

int main() {

    Student stu = { "张三",18,100};
    //值传递
    printStudent(stu);
    cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;

    cout << endl;

    //地址传递
    printStudent2(&stu);
    cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age  << " 分数:" << stu.score << endl;

    system("pause");

    return 0;
}


七 . 结构体中的const

作用: 用const来防止误操作

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

//学生结构体定义
struct Student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};

//const使用场景
void printStudent(const Student *stu) //加const防止函数体中的误操作
{
    //stu->age = 100; //操作失败,因为加了const修饰
    cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;

}

int main() {

    Student stu = { "张三",18,100 };

    printStudent(&stu);

    return 0;
}


八 . 结构体案例

案例1 :

案例描述:

学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下

设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员

学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值

最终打印出老师数据以及老师所带的学生数据。

//**案例描述:**
//
//学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下
//
//        设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员
//
//        学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值
//
//        最终打印出老师数据以及老师所带的学生数据。
//
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

struct Student{
    string sname;
    int score;
};

struct Teacher{
    string tname;
    struct Student stuArray[5];
};

void setInfo(struct Teacher teaArray[] , int len){
    string letter = "ABCDE";
    for(int i = 0 ; i < len ; i++){
        teaArray[i].tname = "Teacher_";
        teaArray[i].tname += letter[i];

        for(int j = 0 ; j < 5 ; j++){
            teaArray[i].stuArray[j].sname = "Student_";
            teaArray[i].stuArray[j].sname += letter[i];
            teaArray[i].stuArray[j].sname += letter[j];
            teaArray[i].stuArray[j].score = rand() % 61 + 40;;
        }
    }
}

void printInfo(struct Teacher teaArray[] , int len){
    for(int i = 0 ; i < len ; i++){
        cout<< teaArray[i].tname <<" : " << endl;
        for(int j = 0 ; j < 5 ; j ++){
            cout<<"\t" << teaArray[i].stuArray[j].sname << " "<<teaArray[i].stuArray[j].score<<endl;
        }
    }
}

int main(){

    //设立随机数种子
    srand((unsigned int)time(NULL));

    //创建数组存放三名老师
    Teacher teaArray[3];

    //计算老师数组的长度
    int len = sizeof(teaArray) / sizeof(teaArray[0]);

    //通过函数给老师和学生赋值
    setInfo(teaArray , len);

    //打印数据
    printInfo(teaArray , len);
}

运行结果 :
在这里插入图片描述

案例2 :

案例描述:

设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。

通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。

五名英雄信息如下:

		{"刘备",23,"男"},
		{"关羽",22,"男"},
		{"张飞",20,"男"},
		{"赵云",21,"男"},
		{"貂蝉",19,"女"},
//**案例描述:**
//
//设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
//
//通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
//
//五名英雄信息如下:
//{"刘备",23,"男"},
//{"关羽",22,"男"},
//{"张飞",20,"男"},
//{"赵云",21,"男"},
//{"貂蝉",19,"女"},

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

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

void BubbleSort(struct Hero h[] , int len){
    for(int i = 0 ; i < len - 1;i++){
        for(int j = 0 ; j < len - i - 1 ; j ++){
            if(h[j].age > h[j + 1].age){
//                struct Hero temp = h[j];
//                h[j] = h[j + 1];
//                h[j + 1] = temp;
                swap(h[j] , h[j + 1]);
            }
        }
    }
}

void printResult(struct Hero h[] , int len){
    for(int i = 0 ; i < len ; i++){
        cout<<h[i].name << " " << h[i].age << " "<<h[i].sex<<endl;
    }
}

int main(){

    //创建结构体数组
    Hero h[5] = {
        {"刘备",23,"男"},
        {"关羽",22,"男"},
        {"张飞",20,"男"},
        {"赵云",21,"男"},
        {"貂蝉",19,"女"},
    };

    //获取数组长度
    int len = sizeof(h) / sizeof(h[0]);

    //打印输出
    printResult(h , len);
    cout<<endl;
    //冒泡排序
    BubbleSort(h , len);

    //打印输出
    printResult(h , len);

}

运行结果 :
在这里插入图片描述



参考资料 :

https://www.bilibili.com/video/BV1et411b73Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值