IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【学习笔记】C++ 结构体和结构体指针 -> 正文阅读

[C++知识库]【学习笔记】C++ 结构体和结构体指针

目录

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

2.定义结构体&声明结构体

3.结构体数组

4.结构体指针

5.结构体嵌套结构体

6.结构体作为函数参数

7.结构体应用


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

2.定义结构体&声明结构体

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

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

struct Student //一些类型的集合
{
    string name;

    int age;

    int score;
} s3; //s3为定义时同时创建一个student类型的结构体

//创建方式
//1. struct Student s1;
//2. struct Student s2 ={....};
//3.在结构体最后顺便定义

int main()
{
    //方法一
    struct Student s1;//struct在创建时关键字struct可以省略
    //给S1成员赋值

    s1.name = "张三";

    s1.age = 10;

    s1.score = 100;

    //方法二

    struct Student s2 = {"李四", 12, 99}; //按照结构体中的定义顺序给成员赋值

    cout << s1.name << " 年龄" << s1.age << " 成绩" << s1.score << endl;

    cout << s2.name << " 年龄" << s2.age << " 成绩" << s2.score << endl;

    system("pause");
    return 0;
}

3.结构体数组

语法:

struct 结构体名 数组名【元素个数】 ={? ?{... ,...? },{},{}? };

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

struct Student //一些类型的集合
{
    string name;

    int age;

    int score;
} ; 



int main()
{
    //创建结构体数组
    struct Student s1[3]=
    {
        {"张三",3,120},
        {"李四",3,120},
        {"张三",3,120}

    };
    s1[2].name ="HSL";
    s1[2].age =88;

    

  

    cout << s1[0].name << " 年龄" << s1[0].age << " 成绩" << s1[0].score << endl;

    cout << s1[2].name << " 年龄" << s1[2].age << " 成绩" << s1[2].score << endl;

    system("pause");
    return 0;
}

4.结构体指针

为结构体创建指针,数据类型为结构体类型

语法: strcut 结构体名 *指针名 =&结构体变量;

注意当指针指向结构体数组时,不用&和->,而是直接使用 .变量名

结构体中含有数组时,方法不变,在变量名后加【元素序号】

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

struct Student //一些类型的集合
{
    string name;

    int age;

    int score[2];
};

int main()
{
    //创建结构体变量(数组)
    struct Student s1[3] =
        {
            {"张三", 3, {120,100}},
            {"李四", 3, {120,32}},
            {"", 3, {120,35}}

        };
    s1[2].name = "HSL";
    s1[2].age = 88;
    struct Student s2 = {"lio", 10, {110,20}};

    //创建指针指向结构体变量(数组)
    struct Student *p = s1; //数组型结构体不用加&
    // 使用指针指向结构体数组不用->,实质:数组名本质是一种指针
    cout << p[2].name << " 年龄" << p[2].age << " 成绩" << p[2].score[0] << endl;

    p = &s2; //指向普通结构体变量
    //
    cout << p->name << " 年龄" << p->age << " 成绩" << p->score[1] << endl;

    system("pause");
    return 0;
}

5.结构体嵌套结构体

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

struct Student //一些类型的集合
{
    string name;

    int age;

    int score;
};

//结构体嵌套
struct teacher
{
    int id;

    struct Student stu;
};

int main()
{

    struct teacher t;

    t.id = 10;

    t.stu.name = " wang ";

    t.stu.age = 10;

    t.stu.score = 100;

    cout << t.id << " name" << t.stu.name << " 成绩" << t.stu.score << endl;

    system("pause");
    return 0;
}

6.结构体作为函数参数

1.值传递

2.地址传递

3.结构体数组的地址传递

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

struct Student //一些类型的集合
{
    string name;

    int age;

    int score;
} s1, s2 ;

//值传递
void printmesg1(struct Student s)
{
    cout << s.name << "年龄" << s.age << " 成绩" << s.score << endl;
}

//地址传递
void printmesg2(struct Student *p)
{
    
    cout << p->name << "年龄" << p->age << " 成绩" << p->score << endl;
    p->name = "t";
}

//结构体数组的地址传递
void printmesg3(const struct Student *p)//加入const可以保护元数据不被误修改
{
    cout << p[0].name << "年龄" << p[0].age << " 成绩" << p[0].score << endl;
}
int main()
{

    s1 = {"hsl", 20, 100};
    s2 = {"hr", 18, 98};
    Student s3[2] = {{"thr", 22, 100}, {"hsl", 22, 120}};

    printmesg1(s1);

    printmesg2(&s2);

    printmesg3(s3);

    cout << s2.name << "年龄" << s2.age << " 成绩" << s2.score << endl;//地址传递改变值,实参也会变

    system("pause");
    return 0;
}

7.结构体应用

要求:

一个老师有三个学生,使用结果结构体打印出三个老师的姓名和他们学生的姓名,成绩

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

struct Student //一些类型的集合
{
    string name;

    int score;
};

struct teacher
{
    string name;

    Student arrstu[3];
};

void allocateSpace(struct teacher *arr, int len)//arr[]=*arr
{
    string nameSeed = "ABCDE";
    for (int i = 0; i < len; i++)
    {

        arr[i].name = nameSeed[i];

        for (int j = 0; j < 3; j++)
        {
            arr[i].arrstu[j].name = nameSeed[j];
            arr[i].arrstu[j].score = rand()%61+40;
        }
    }
}

void printInfo(struct teacher arr[], int len)
{

    for (int i = 0; i < len; i++)
    {

        cout << "老师:" << arr[i].name << endl;

        for (int j = 0; j < 3; j++)
        {
            cout << "\t学生:"
                 << arr[i].arrstu[j].name
                 << "分数:" << arr[i].arrstu[j].score << endl;
        }
    }
}

int main()
{
    // 随机数种子
    srand((unsigned int)time(NULL));

    struct teacher tArray[3];

    int len = sizeof(tArray) / sizeof(tArray[0]);

    allocateSpace(tArray, len);

    printInfo(tArray, len);

    system("pause");
    return 0;
}

要求:

对学生成绩进行降序排列,顺序打印。

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

struct Student //一些类型的集合
{
    string name;

    int age;

    int score;
};

void printInfo(struct Student arr[], int len)
{

    for (int i = 0; i < len; i++)
    {

        cout << "name:" << arr[i].name << "  age: " << arr[i].age << "  score: " << arr[i].score << endl;
    }
}

void bubbleSort(struct Student *arr, int len)
{
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = 0; j < len - 1 - i; j++)
        {
            struct Student temp;
            if (arr[j].score < arr[j + 1].score)
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main()
{
    // 随机数种子
    Student s1[5] =
        {{"张飞", 38, 15}, {"留被", 54, 59}, {"关于", 22, 98}, {"貂蝉", 18, 75}, {"沙雕", 100, 0}

        };
    int len = sizeof(s1) / sizeof(s1[0]);

    bubbleSort(s1, len);

    printInfo(s1, len);

    system("pause");
    return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-02 10:37:50  更:2021-08-02 10:38:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 14:21:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码