C++学习 --- 结构体

1、结构体定义:

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

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

#include <iostream>
#include <string>
using namespace std;
​
//1.创建学生数据类型
//学生:姓名、年龄、分数
struct Student{
    string name; //姓名
    int age;     //年龄
    float score; //分数
}s3;
​
//2.通过学生的类型创建具体的学生
int main(){
    //方法1,创建结构体
    //struct Student s1; 
    Student s1; //struct 关键字可以省略
    s1.name = "Susan";
    s1.age = 18;
    s1.score = 100;
    cout << "name: " << s1.name <<", age: " << s1.age << ", score:" << s1.score<< endl;
    
    //方法2,创建结构体
    struct Student s2 = {"Lisoy",16,99.7};
    cout << "name: " << s2.name <<", age: " << s2.age << ", score:" << s2.score<< endl;
    
    //方法3,创建结构体时顺便创建结构体变量
    s3.name = "Wazoo";
    s3.age = 15;
    s3.score = 98.6;
    cout << "name: " << s3.name <<", age: " << s3.age << ", score:" << s3.score<< endl;
    
    return 0;
}

 

2、结构体数组

作用:将自定义结构体 放入到数组中,方便维护

语法: struct 结构体名 数组名[元素个数] { {结构体成员列表}, {}, ... {} };

#include <iostream>
#include <string>
using namespace std;
​
//1.结构体定义
struct Student{
    //成员列表
    string name; //姓名
    int age;     //年龄
    float score; //分数
};
​
​
int main(){
    
    //2.创建结构体数组
    struct Student stuArray[4] = {
        {"Susan",18,100},
        {"Lisoy",16,99.7},
        {"Wazoo",15,98.6},
    };
    
    //3.给结构体数组中的元素赋值
    stuArray[3].name = "Ziser";
    stuArray[3].age = 90;
    stuArray[3].score = 98.9;
    
    //4.遍历结构体数组
    for(int i = 0;i < 4;i++){
        cout << "name:" << stuArray[i].name << ", age:"<< stuArray[i].age <<", score:"<< stuArray[i].score << endl;
    }
    return 0;
}

 

 

3、结构体指针

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

利用操作符 ->

#include <iostream>
#include <string>
using namespace std;
​
//1.结构体定义
struct Student{
    //成员列表
    string name; //姓名
    int age;     //年龄
    float score; //分数
};
​
​
int main(){
    //2.创建学生的结构体变量 (struct 可省略)
    Student s = {"Susan",18,100};
    
    //3.通过指向结构体变量 (struct 可省略)
    Student * p = &s;
    
    //4.通过指针访问结构体变量中的数据
    cout << "name: " << p->name <<", age: " << p->age << ", score:" << p->score<< endl;
    
    return 0;
}

 

4、结构体嵌套结构体

用于解决实际问题。

 

#include <iostream>
#include <string>
using namespace std;
​
//1.学生结构体定义
struct Student{
    //成员列表
    string name; //学生姓名
    int age;     //学生年龄
    float score; //学生分数
};
​
//2.教师结构体定义
struct Teacher{
    //成员列表
    int id;             //教师编号
    string name;        //教师姓名
    int age;            //教师年龄
    struct Student stu; //辅导的学生
};
int main(){
    //3.创建老师
    Teacher t;
    t.id = 001;
    t.name = "Einstein";
    t.age = 50;
    t.stu.name = "Zeus";
    t.stu.age = 24;
    t.stu.score = 94.56;
    
    //4.访问结构体变量中的数据
    cout << "Teacher's id: " << t.id << ", Teacher's name: " << t.name <<", Teacher's age: " << t.age << endl;
    cout << "Student's name: " << t.stu.name <<", Student's age: " << t.stu.age << ", Student's score:" << t.stu.score<< endl;
    
    return 0;
}

 

5、结构体作为函数参数

#include <iostream>
#include <string>
using namespace std;
​
//1.学生结构体定义
struct Student{
    //成员列表
    string name; //学生姓名
    int age;     //学生年龄
    float score; //学生分数
};
​
//4.值传递
void printStudent1(struct Student s){
    s.age = 100;
    cout << "printStudent1" <<endl;
    cout << "name: " << s.name <<", age: " << s.age << ", score:" << s.score<< endl;
    return;
}
​
//5.地址传递
void printStudent2(struct Student * s){
    cout << "printStudent2" <<endl;
    s->age = 90;
    cout << "name: " << s->name <<", age: " << s->age << ", score:" << s->score<< endl;
    return;
}
int main(){
​
    //2.创建结构体
    struct Student s;
    s.name = "Susan";
    s.age = 18;
    s.score = 100;
    
    //3.打印结构体数据
    //cout << "name: " << s.name <<", age: " << s.age << ", score:" << s.score<< endl;
    
    //3.通过函数打印结构体信息
    printStudent1(s);
    printStudent2(&s);
    
    //6. 验证 age被printStudent2 中修改
    cout << "name: " << s.name <<", age: " << s.age << ", score:" << s.score<< endl;
    
    return 0;
}
​
//结论 :值传递 拷贝数据为另一份,拷贝数据量大时,占用内存;地址传递 和原数据是同一份数据

 

6、用const修饰结构体, 防止误操作

#include <iostream>
#include <string>
using namespace std;
​
//1.学生结构体定义
struct Student{
    //成员列表
    string name; //学生姓名
    int age;     //学生年龄
    float score; //学生分数
};
​
​
//3.选地址传递,为防止误操作且占用较少内存的方式
void printStudent2(const struct Student * s){
    cout << "printStudent2" <<endl;
    //s->age = 90; //防止误操作,表达式必须是可修改的左值
    cout << "name: " << s->name <<", age: " << s->age << ", score:" << s->score<< endl;
    return;
}
​
int main(){
    //2.创建结构体
    struct Student s = {"Susan",18,100};
​
    //3.通过函数打印结构体信息
    printStudent2(&s);
​
    return 0;
}

 

7、结构体案例

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
​
//1.学生结构体定义
struct Student {
    //成员列表
    string sName; //学生姓名
    int score;  //学生分数
};
​
//2.教师结构体定义
struct Teacher {
    //成员列表
    string tName;             //教师姓名
    struct Student sArray[5]; //辅导的学生数组
};
​
//给老师和学生赋值的函数
void allocateSpace(Teacher tArray[], int len) {
    string nameSeed = "ABCDE";
​
    //给老师属性开始赋值
    for (int i = 0;i < len;i++) {
        tArray[i].tName = "Teacher_";
        tArray[i].tName += nameSeed[i];
​
        for (int j = 0;j < 5; j++) {
            tArray[i].sArray[j].sName = "Student_";
            tArray[i].sArray[j].sName += nameSeed[j];
            int random = rand() % 61 + 40;
            tArray[i].sArray[j].score = random;
        }
    }
    return;
}
​
//打印所有信息
void printInfo(Teacher tArray[], int len) {
    for (int i = 0;i < len;i++) {
        cout << "Teacher's name:" << tArray[i].tName << " : " << endl;
        for (int j = 0; j < 5; j++) {
            cout << "\tStudent's name:" << tArray[i].sArray[j].sName << ", Student's score:" << tArray[i].sArray[j].score << endl;
        }
        cout << endl;
    }
    return;
}
​
int main() {
​
    //随机数种子,需指定头文件 ctime
    srand((unsigned int)time(NULL));
​
    //3.创建老师结构体数组,长度为3
    Teacher tArray[3];
    int len = sizeof(tArray) / sizeof(tArray[0]);
    
    //4.通过函数给每位老师及所带的学生赋值
    allocateSpace(tArray, len);
​
    //5.打印老师数据以及老师所带学生数据
    printInfo(tArray, len);
    
    system("pause");
    return 0;
}

 

8、结构体案例

#include <iostream>
#include <string>
using namespace std;
​
//1.设计英雄结构体
struct Hero {
    string name;//姓名
    int age;    //年龄
    string sex; //性别
};
​
​
//排序
void bubbleSort(Hero heroArray[], int len) {
    for (int i = 0;i < len - 1; i++) {
        for (int j = 0;j < len - i - 1; j++) {
            //下标 j > j+1 的元素值,交换
            if (heroArray[j].age > heroArray[j + 1].age) {
                Hero temp = heroArray[j];
                heroArray[j] = heroArray[j+1];
                heroArray[j+1] = temp;
            }
        }
    }
    return;
}
​
​
//打印
void printHero(Hero heroArray[], int len) {
    for (int i = 0; i < len; i++) {
        cout << "姓名:" << heroArray[i].name << ", 年龄:" << heroArray[i].age << ", 性别:" << heroArray[i].sex << endl;
    }
    return;
}
​
int main() {
    
    //2.创建数组存放5名英雄
    Hero heroArray[5] = {
        {"刘备",23,""},
        {"关羽",22,""},
        {"张飞",20,""},
        {"赵云",21,""},
        {"貂蝉",19,""}
    };
    
    int len = sizeof(heroArray) / sizeof(heroArray[0]);
    /*for (int i = 0; i < len; i++) {
        cout << "姓名:" << heroArray [i].name << ", 年龄:" << heroArray[i].age << ", 性别:" << heroArray[i].sex << endl;
    }*/
    
    //3.对数组进行排序,按照年龄升序
    bubbleSort(heroArray,len);
    
    //4.打印
    printHero(heroArray, len);
    
    return 0;
}
posted @ 2021-08-02 18:13  yiwenzhang  阅读(620)  评论(0编辑  收藏  举报