写在前面:
- 本系列专栏主要介绍C++的相关知识,思路以下面的参考链接教程为主,大部分笔记也出自该教程,笔者的原创部分主要在示例代码的注释部分。
- 除了参考下面的链接教程以外,笔者还参考了其它的一些C++教材(比如计算机二级教材和C语言教材),笔者认为重要的部分大多都会用粗体标注(未被标注出的部分可能全是重点,可根据相关部分的示例代码量和注释量判断,或者根据实际经验判断)。
- 由于C++基本继承了C语言的所有内容,建议读者先阅读C语言系列的专栏,有一些重点是仅在C语言系列专栏中有介绍的(比如二级指针、预处理等)。
- 如有错漏欢迎指出。
参考教程:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili
一、结构体概述
1、结构体基本概念
结构体属于用户自定义的数据类型,允许用户在同一个结构体变量中存储不同的类型的数据。
2、结构体的定义
(1)建立结构体类型的语法:
struct <结构体名> { <结构体成员列表> };
(2)定义结构体类型变量的三种方法:
①先声明结构体类型,再定义该类型的变量。
struct <结构体名> <变量名>;
②在声明类型的同时定义变量。
struct <结构体名>
{
<结构体成员列表>
}<变量名列表>;
③不指定类型名而直接定义结构体类型变量。(使用较少)
struct
{
<结构体成员列表>
}<变量名列表>;
(3)定义结构体时的关键字是struct,不可省略;创建结构体变量时,关键字struct可以省略。
3、结构体变量的初始化和访问
(1)在定义结构体变量时可以对它的成员初始化,初始化列表是用花括号括起来的一些数据(逗号分隔),这些数据依次赋给结构体变量中的各成员。
(2)可以访问结构体变量中成员的值,访问方式为:
<结构体变量名>.<成员名>
(3)举例:
#include<iostream>
using namespace std;
#include<string>
//1、创建学生数据类型 学生包括姓名、年龄、分数三个属性
struct student
{
//成员列表
string name;
int age;
int score; //一些类型集合组成的一个类型
};
//}s3; //定义结构体时可顺便创建变量,不建议使用
//2、通过学生类型创建具体学生
//①struct 结构体名 变量名
//struct student s1;
//②struct 结构体名 变量名 = { 成员1值 , 成员2值... }
//struct student s2 = { "张三",18,100 };
//③定义结构体时顺便创建变量
int main() {
struct student s1; //struct关键字可以省略
s1.name = "张三"; //给s1属性赋值,通过.访问结构体变量中的属性
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << endl;
struct student s2 = { "李四",18,100 };
cout << "姓名:" << s2.name << " 年龄:" << s2.age << endl;
system("pause");
return 0;
}
二、结构体数组
1、定义结构体数组
(1)结构体数组的每个元素都是一个结构体类型的数据,它们都分别包括各个成员项。
(2)定义结构体数组的两种形式:
struct <结构体名>
{
<结构体成员列表>
}<数组名>[<数组长度>];
<结构体名> <数组名>[<数组长度>];
(3)对结构体数组初始化的形式与对普通数组初始化的形式类似,只是每个元素都是结构体常量,用花括号括起,花括号内的是赋给结构体元素各成员的值。
2、举例
#include<iostream>
using namespace std;
#include<string>
//结构体定义
struct student
{
//成员列表
string name;
int age;
int score;
};
int main() {
//结构体数组
struct student arr[3]=
{
{"张三",19,90},
{"李四", 18, 100},
{"王五",20,80}
};
arr[2].age = 21; //可以修改数组里某个结构体元素的某个成员
for (int i = 0; i < 3; i++)
{
cout << "姓名: " << arr[i].name << " 年龄: "
<< arr[i].age << endl;
}
system("pause");
return 0;
}
三、结构体指针
1、指向结构体变量的指针
(1)指针变量的基类型必须与结构体变量的类型相同。
(2)利用操作符“->”可以通过结构体指针访问结构体属性(也可以将结构体指针解引用,再用操作符“.”一样可以访问结构体属性)。
(3)定义结构体指针的两种形式:
struct <结构体名>
{
<结构体成员列表>
}*<指针名>;
<结构体名> *<指针名>;
#include<iostream>
using namespace std;
#include<string>
//结构体定义
struct student
{
string name;
int age;
int score;
};
int main() {
struct student stu = { "张三",19,90 };
//int * p = &s ;
struct student *p = &stu; //struct关键字可以省略
cout << "姓名: " << p->name << " 年龄: "
<< p->age << endl; //通过指针访问结构体变量中的数据
cout << "姓名: " << (*p).name << " 年龄: "
<< (*p).age << endl;
system("pause");
return 0;
}
2、指向结构体数组的指针
(1)指针变量的基类型必须与结构体变量的类型相同。
(2)可以用指针变量指向结构体数组的元素,和普通数组一样,只是指向结构体数组元素的指针还可以访问结构体元素的成员。
#include<iostream>
using namespace std;
#include<string>
//结构体定义
struct student
{
string name;
int age;
int score;
};
int main() {
struct student stu[2] = { { "张三",19,90 }, { "张四",14,50 } };
//int * p = &s ;
struct student *p = stu; //指针指向数组首元素
cout << "姓名: " << p->name << " 年龄: "
<< p->age << endl;
p++; //指针指向下一个元素
cout << "姓名: " << (*p).name << " 年龄: "
<< (*p).age << endl;
system("pause");
return 0;
}
四、结构体嵌套结构体
1、概述
(1)结构体中的成员可以是另一个结构体,在结构体中可以定义另一个结构体作为成员。
(2)需要注意的是,结构体中的成员类型不可以是结构体的本身类型,不过可以是结构体本身类型的指针。
2、举例
#include<iostream>
using namespace std;
#include<string>
//学生结构体定义
struct student
{
string name;
int age;
int score;
};
//老师结构体定义
struct teacher
{
int id;
string name;
int age;
struct student stu; //老师带的学生
};
int main() {
//创建老师
teacher t;
t.id = 10000;
t.name = "老王";
t.age = 50;
t.stu = { "张三",30,60 };
t.stu.age = 60;
cout << "老师姓名:" << t.name << " 老师编号:" << t.id << " 老师辅导的学生的姓名:"
<< t.stu.name << endl;
system("pause");
return 0;
}
五、结构体做函数参数
将结构体作为参数向函数中传递,其中传递方式有值传递和地址传递两种,如果不想修改主函数中的数据就用值传递,反之用地址传递。
#include<iostream>
using namespace std;
#include<string>
//学生结构体定义
struct student
{
string name;
int age;
int score;
};
//值传递
void printstudent1(student stu)
{
cout << "子函数(值传递)中打印年龄:" << stu.age << endl;
}
//地址传递
void printstudent2(student *p)
{
cout << "子函数(地址传递)中打印年龄:" << p->age << endl;
}
int main() {
struct student s;
s.name = "张三";
s.age = 20;
s.score = 85;
cout << "main函数中打印年龄:" << s.age << endl;
printstudent1(s);
printstudent2(&s);
system("pause");
return 0;
}
六、结构体中const使用场景
const修饰函数参数,当参数使用地址传递时可以防止子函数对实参进行修改。
#include<iostream>
using namespace std;
#include<string>
//学生结构体定义
struct student
{
string name;
int age;
int score;
};
//地址传递可以减少内存空间,不会复制新的“副本”出来
void printstudent(const student *p)
{
//p->age = 150; 不能修改结构体的值了,否则会报错
cout << "子函数(地址传递)中打印年龄:" << p->age << endl;
}
int main() {
struct student s;
s.name = "张三";
s.age = 20;
s.score = 85;
printstudent(&s);
cout << "main中张三的年龄为" << s.age << endl;
system("pause");
return 0;
}
七、案例
1、案例1
(1)学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
①设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员。
②学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值。
③最终打印出老师数据以及老师所带的学生数据。
(2)源码:
#include<iostream>
using namespace std;
#include<string>
/*
案例描述:
学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下:
①设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员
②学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值
③最终打印出老师数据以及老师所带的学生数据
*/
struct student
{
string name;
int score;
};
struct teacher
{
string name;
struct student stu[5];
};
void allocapespace(struct teacher t[],int lens,int lent)
{
string numberT = "ABC";
string numberS = "12345";
for (int i = 0; i < lent; i++)
{
t[i].name = "Teacher_";
t[i].name = t[i].name + numberT[i];
for (int j = 0; j < lens; j++)
{
t[i].stu[j].name = "Student_";
t[i].stu[j].name = t[i].stu[j].name + numberS[j];
t[i].stu[j].score = 60 + j * 6; //随便赋分
}
}
}
int main() {
teacher t[3] ;
int lens = sizeof(t[0].stu) / sizeof(t[0].stu[0]);
int lent = sizeof(t) / sizeof(t[0]);
{
allocapespace(t, lens ,lent);
}
for (int i = 0; i < lent; i++)
{
cout << t[i].name << "带的学生:" << endl;
for (int j = 0; j < lens; j++)
{
cout << t[i].stu[j].name << "分数:" << t[i].stu[j].score << endl;
}
}
system("pause");
return 0;
}
2、案例2
(1)案例描述:
①设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
②通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
③五名英雄信息如下:{"刘备",23,"男"},{"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"}。
(2)源码:
#include<iostream>
using namespace std;
#include<string>
/*
案例描述:
①设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄
②通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果
③五名英雄信息如下:{"刘备",23,"男"},{"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"}
*/
struct hero
{
string name;
int age;
string sex;
};
int main() {
hero h[5] = { {"刘备",23,"男"},{"关羽",22,"男"}, {"张飞",20,"男"}, {"赵云",21,"男"}, {"貂蝉",19,"女"} };
int len = sizeof(h) / sizeof(h[0]);
struct hero temp;
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)
{
temp = h[j];
h[j] = h[j + 1];
h[j + 1] = temp;
}
}
}
for (int i = 0; i < len ; i++)
{
cout << h[i].name << ": 年龄--" << h[i].age << "岁 性别--" << h[i].sex << endl;
}
system("pause");
return 0;
}