C ++ステージ01ノート08 [構造(基本概念、定義と使用、配列、ポインター、ネスト、const使用)]

C ++ | 0から1の入門プログラミングまでの独創的な作業[ビデオ+コースウェア+メモ+ソースコード]

目次

8構造

8.1構造の基本概念

8.2構造の定義と使用

8.3構造体配列

8.4構造体ポインタ

8.5構造ネストされた構造

8.6関数パラメータとしての構造

8.7構造体でconstを使用するためのシナリオ

8.8構造の場合

8.8.1ケース1

8.8.2ケース2


8構造

8.1構造の基本概念

構造はユーザー定義のデータ型あり、ユーザーはさまざまなデータ型を格納できます。

C ++組み込みデータ型:int、float、double、string、char .. ..

8.2構造の定義と使用

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

構造を介して変数を作成する方法は3つあります。

  1. 構造体構造体名変数名

  2. 構造体構造体名変数名= {メンバー1の値、メンバー2の値...}

  3. 構造を定義するときは、ところで変数を作成してください

  

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

//1、创建学生数据类型:学生包括(姓名、年龄、分数)
//自定义数据类型,一些数据类型集合组成的一个类型(其实就是内置数据类型的集合)。
//语法:struct 类型名称 { 成员列表 }

//结构体定义
struct student //结构体名称-见名知意
{
	//成员列表---学生属性
	string name; //姓名
	int age;	 //年龄
	int score;	 //分数
} stu3;	//结构体变量创建方式3

//2、通过学生类型创建具体学生(共有3种方式)

//2.1、struct student s1; // s1就是变量名
//2.2、struct student s2 = {...}; // 创建变量的同时赋初值
//2.3、在定义结构体时顺便创建结构体变量

int main()
{
	//【结构体变量创建方式1】2.1、struct student s1;
	struct student stu1; //创建结构体时,struct关键字可以省略
	//给stu1属性赋值,通过“.”访问结构体变量中的属性
	stu1.name = "张三";
	stu1.age = 18;
	stu1.score = 100;
	//方式1打印输出
	cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 分数:" << stu1.score << endl;

	//【结构体变量创建方式2】2.2、struct student s2 = {...};
	struct student stu2 = {"李四", 19, 60};
	//方式2打印输出
	cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 分数:" << stu2.score << endl;

	//【结构体变量创建方式3】在定义结构体时顺便创建结构体变量,不建议使用
	stu3.name = "王五";
	stu3.age = 18;
	stu3.score = 80;
	//方式3打印输出
	cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 分数:" << stu3.score << endl;

	system("pause");
	return 0;
}
  • まとめ1:構造体を定義する際のキーワードはstructであり、省略できません。
  • 要約2:構造体変数を作成する場合、キーワードstructは省略できます。
  • 概要3:構造体変数は、演算子「」を使用してメンバーにアクセスします。

8.3構造体配列

機能:メンテナンス容易にするためにカスタム構造配列に配置ます。

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

 

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

//结构体数组

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

int main()
{
	//2、创建结构体数组
	struct student stuArray[3] =
	{
		{"张三", 18, 88},
		{"李四", 28, 99},
		{"王五", 38, 66}
	};

	//3、给结构体数组中的元素赋值
	stuArray[2].name = "赵六";
	stuArray[2].age = 66;
	stuArray[2].score = 99;

	//4、遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stuArray[i].name
			 << "\t年龄:" << stuArray[i].age
			 << "\t分数:" << stuArray[i].score << endl;
	}

	system("pause");
	return 0;
}

8.4構造体ポインタ

役割:ポインターを介して構造体のメンバーにアクセスます//構造体ポインタを使用して構造体メンバー操作します。

  • 演算子->を使用すると、ポインタアクセス構造のプロパティを構造化できます

 &s:sのアドレスを取得し、sに対応するデータ型のポインタを返します。

 

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

//结构体指针

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

int main()
{
	//2、创建学生结构体变量
	// struct student stu = {"张三", 25, 100}; //struct可以省略
	student stu = {"张三", 25, 99}; //struct可以省略

	//3、通过指针指向结构体变量
	// struct student *p = &stu; //struct可以省略
	student *p = &stu; //struct可以省略

	//结构体变量,可以直接通过.来访问
	cout << "姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;

	//4、通过指针访问结构体变量中的数据
	p->score = 66; //指针通过 -> 操作符可以访问成员

	//通过结构体指针访问结构体中的属性,需要利用“->”
	cout << "姓名:" << p->name << "\t年龄:" << p->age << "\t分数:" << p->score << endl;

	system("pause");
	return 0;
}
  • 概要:構造体ポインターは、->演算子を使用して構造体のメンバーにアクセスできます。

8.5構造ネストされた構造

役割: 構造体のメンバー別の構造体にすることができます

例:各教師が生徒を指導し、教師の構造が生徒の構造を記録します。

#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; //教师辅导的学生 子结构体
};

int main()
{
	//创建教师结构体变量
	struct teacher t1;
	t1.id = 333;
	t1.name = "老王";
	t1.age = 50;

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

	cout << "教师编号:" << t1.id
		 << "\t教师姓名:" << t1.name
		 << "\t教师年龄:" << t1.age
		 << "\t教师辅导的学生姓名:" << t1.stu.name
		 << endl;

	cout << "学生姓名:" << t1.stu.name
		 << "\t学生年龄:" << t1.stu.age
		 << "\t学生考试分数:" << t1.stu.score
		 << endl;

	system("pause");
	return 0;
}

概要:構造体では、実際の問題を解決するために、別の構造体をメンバーとして定義できます。

8.6関数パラメータとしての構造

役割:構造体をパラメーターとして関数に渡します。

配送には2つの方法があります。

  1. 値渡し

  2. アドレスパス

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

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

//打印学生信息函数
//1、值传递
void printStudent1(struct student stu) //(student stu)
{
	stu.age = 28; //修改属性
	cout << "1子函数中\t姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
}

//2、地址传递
void printStudent2(struct student *stu) //用指针*stu接收地址
{
	stu->age = 28;
	cout << "2子函数中\t姓名:" << stu->name << "\t年龄:" << stu->age << "\t分数:" << stu->score << endl;
}

int main()
{
	//结构体作函数参数
	//将学生传入到一个参数中,打印学生身上的所有信息

	//创建结构体变量
	student stu = {"张三", 18, 100};
	stu.name = "李四";
	stu.age = 20;
	stu.score = 85;

	//1、值传递
	printStudent1(stu);
	cout << "1主函数中\t姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;

	cout << endl;

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

	system("pause");
	return 0;
}

概要:main関数のデータを変更したくない場合は、値で渡します。それ以外の場合は、アドレスで渡します。

8.7構造体でconstを使用するためのシナリオ

役割:誤操作を防ぐためにconstを使用します。

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

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

/*
 * 值传递
 * stu.age = 150;//mian中的stu变量不会改变
 * 会将实参中的数据拷贝一份,放在形参s上
 * 无论形参如何改变,都不会影响实参
 * 1、实参有很多属性,每个属性都拷贝一份,拷贝的数据量很大
 * 2、假设一个学校有成千上万个人,每个人都调用printStudent函数,拷贝成千上万份数据,
 *    这样数据量就会非常大,占用内存空间大
 */
void printStudent1(student stu) // (struct student stu) 省略struct
{
	stu.age = 150; //mian中的stu变量不会改变
	cout << "姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
}

// void printStudent3(const student stu) // (struct student stu) 省略struct
// {
// 	stu.age = 150; //mian中的stu变量不会改变
// 	cout << "姓名:" << stu.name << "\t年龄:" << stu.age << "\t分数:" << stu.score << endl;
// }

//将函数中的形参改为指针,可以减少内存空间的使用,而且不会复制新的副本出来(值传递会复制整个结构体元素)
//const使用场景
void printStudent2(const student *stu) //加const防止函数体中的误操作 指针*stu节省空间,一个指针占4个字节内存
{
	//stu->age = 100; //操作失败,因为加了const修饰,常量指针无法修改指针指向的值,只能读不能写。防止age的值会被修改
	//加入const之后,一旦有修改的操作就会报错,可以防止我们的误操作(加“const”防止误操作)
	cout << "姓名:" << stu->name << "\t年龄:" << stu->age << "\t分数:" << stu->score << endl;
}

int main()
{
	//创建结构体变量
	student stu = {"张三", 18, 100};

	printStudent1(stu); //值传递

	//通过函数打印结构体变量信息
	printStudent2(&stu); //传入地址,在函数中指针接收地址

	cout << "main()中 张三的年龄为:" << stu.age << endl;

	system("pause");
	return 0;
}

8.8構造の場合

8.8.1ケース1

ケースの説明:

学校は最終プロジェクトに取り組んでいます。各教師は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 sArray[5]; //学生数组
};

//创建数据——给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[], int len) //struct可以省略
{
    string tName = "老师"; // Teacher_
    string sName = "学生";
    string nameSeed = "ABCDE";
    //给老师进行赋值
    for (int i = 0; i < len; i++)
    {
        tArray[i].tName = tName + nameSeed[i]; //老师姓名
        //通过循环给每名老师所带的学生进行赋值
        for (int j = 0; j < 5; j++)
        {
            tArray[i].sArray[j].sName = sName + nameSeed[j];
            tArray[i].sArray[j].score = rand() % 61 + 40; // 0~60 40~100
            // rand() % 60:0~59
        }
    }
}

//打印数据——打印所有信息
void printTeachers(struct Teacher tArray[], int len) //struct可以省略
{
    for (int i = 0; i < len; i++)
    {
        cout << "老师的姓名:" << tArray[i].tName << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << "\t学生姓名:" << tArray[i].sArray[j].sName << ";考试分数:" << tArray[i].sArray[j].score << endl;
        }
    }
}

int main()
{
    //利用系统时间,产生随机数
    srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime>

    //1、创建3名老师的数组
    struct Teacher tArray[3]; //老师数组 struct可以省略

    //2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
    cout << "string类型所占内存空间为:" << sizeof(string) << endl; //string类型所占内存空间为:32
    cout << "int类型所占内存空间为:" << sizeof(int) << endl; //int类型所占内存空间为:4
    cout << "sizeof(tArray):" << sizeof(tArray) << endl;            //sizeof(tArray):696
    cout << "sizeof(Teacher):" << sizeof(Teacher) << endl;          //sizeof(Teacher):232
    cout << "sizeof(Student):" << sizeof(Student) << endl;          //sizeof(Student):40
    cout << "sizeof(tArray[0]):" << sizeof(tArray[0]) << endl;      //sizeof(tArray[0]):232
    int len1 = sizeof(tArray) / sizeof(Teacher);                     //计算数组长度
    cout << "len1:" << len1 << endl;                                //len1:3
    int len2 = sizeof(tArray) / sizeof(tArray[0]);                   //计算数组长度
    cout << "len2:" << len2 << endl;                                //len2:3
    allocateSpace(tArray, len2);                                     //创建数据

    //3、打印所有老师及所带的学生信息
    printTeachers(tArray, len2); //打印数据

    system("pause");
    return 0;
}

8.8.2ケース2

ケースの説明:

メンバー名、年齢、性別を含むヒーロー構造を設計し、構造配列を作成して、5人のヒーローを配列に格納します。

バブルソートアルゴリズムにより、配列のヒーローが年齢の昇順でソートさ、ソートされた結果が最終的に出力されます。

5人のヒーローの情報は次のとおりです。

{"劉備"、23、 "男性"}、
{"
関羽"、22、 "男性"}、{"張飛"、20、 "男性"}、
{"趙雲"、21、 "男性" }、
{"Diao Chan"、19、 "女性"}、

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

//1、设计英雄结构体
//英雄结构体
struct hero
{
    string name; //姓名
    int age;     //年龄
    string sex;  //性别
};

//冒泡排序——实现年龄升序排列
void bubbleSort(struct hero arr[], int len) //struct可省
{
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = 0; j < len - 1 - i; j++)
        {
            //如果j下标的元素年龄大于j+1下标的元素的年龄,交换两个元素
            if (arr[j].age > arr[j + 1].age)
            {
                struct hero temp = arr[j]; //临时数据
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

//打印数组——打印排序后数组中的信息
void printHeros(struct hero arr[], int len) //struct可省
{
    for (int i = 0; i < len; i++)
    {
        cout << "姓名:" << arr[i].name << "\t性别:" << arr[i].sex << "\t年龄:" << arr[i].age << endl;
    }
}

//1、设计英雄结构体
//2、创建数组存放5名英雄
//3、对数组进行排序,按照年龄进行升序排序
//4、将排序后的结果打印输出
int main()
{
    //2、创建数组存放5名英雄
    struct hero heroArray[5] =
        {
            {"刘备", 23, "男"},
            {"关羽", 22, "男"},
            {"张飞", 20, "男"},
            {"赵云", 21, "男"},
            {"貂蝉", 19, "女"}};

    cout << "string类型所占内存空间为:" << sizeof(string) << endl;   //string类型所占内存空间为:32
    cout << "int类型所占内存空间为:" << sizeof(int) << endl;         //int类型所占内存空间为:4
    cout << "sizeof(heroArray):" << sizeof(heroArray) << endl;       //sizeof(heroArray):360
    cout << "sizeof(heroArray[0]):" << sizeof(heroArray[0]) << endl; //sizeof(heroArray[0]):72
    cout << "sizeof(hero):" << sizeof(hero) << endl;                 //sizeof(hero):72

    int len1 = sizeof(heroArray) / sizeof(heroArray[0]); //整体所占空间大小/单个元素所占空间大小
    cout << "len1:" << len1 << endl;                    //len1:5
    int len2 = sizeof(heroArray) / sizeof(hero);         //获取数组元素个数
    cout << "len2:" << len2 << endl;                    //len1:5

    cout << "排序前,数组打印:" << endl;
    for (int i = 0; i < len1; i++)
    {
        cout << "姓名:" << heroArray[i].name << "\t性别:" << heroArray[i].sex << "\t年龄:" << heroArray[i].age << endl;
    }

    //3、对数组进行排序,按照年龄进行升序排序
    bubbleSort(heroArray, len1); //排序

    cout << "排序后,数组打印:" << endl;

    //4、将排序后的结果打印输出
    printHeros(heroArray, len1); //打印

    system("pause");
    return 0;
}

このケース演習の3つのポイント:

  1. 構造体配列の書き方
  2. バブルソート//「バブルソート」を使用して構造体配列をソートします
  3. 構造体入力関数の記述方法、操作構造体の記述方法

おすすめ

転載: blog.csdn.net/weixin_44949135/article/details/115279313
おすすめ