概述
12 结构体
函数是对功能的封装,结构体是对信息的封装
12.1 结构体概念
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
结构体是由一批不同类型数据组合而成的一种新的数据类型,结构体数据的每个数据称为结构体的”成员“(域、元素),通常用来表示类型不同但是又相关的若干数据。
操作方法:
1)定义结构体——描述存储在结构体中的各种数据及类型
2)创建结构体遍历(结构体数据对象),并使用
12.2 结构体的定义和使用
1.结构体定义的格式:
struct 结构体名{`
数据类型 变量名;
…
};
2.创建结构体变量
struct 结构体名 变量名;
结构体名 变量名;
3.访问结构体变量中的成员
结构体变量.成员名;
4.结构体变量的初始化
创建结构体变量的同时进行初始化,值列表
student stu={成员的值1,成员的值2,…};
C语言和C++语言关于结构体的区别:
C++结构体中可以有函数,而C语言中不能有
定义一个描述学生信息的结构体:
struct student{ //struct 为关键字 student 为结构体名
char name[20]; // name age id 为结构体成员
int age;
int id;
}; // 结构体的最后需要带上分号
使用结构体
--创建student结构体的变量:
struct student stu;
student stu;// struct可以省略掉
--访问结构体类型变量成员:
cin>>stu.name;
cout<<stu.name<<endl;
结构体变量的初始化
student stu={
"tom",
20,
1102
};
//或者 省略等号
student stu{"tom",18,1104};
student stu{};//默认为0
结构体之间可以进行赋值,把结构体中的每一个成员都进行赋值
stu1=stu2;
定义结构体的同时创建结构体变量,还可以对结构体变量进行初始化
struct student1{
char name[20];
int age;
}stu3={
"tom",
20
};
12.3 结构体数组
可创建元素为结构体的数组,方法和创建基本类型数组完全相同
student stus[20];
cin>>stus[0].name;
cout<<stus[19].age<<endl;
结构体数组初始化
#include<iostream>
#include<string>
using namespace std;
//定义结构体
struct student{
string name;
int age;
int id;
};
int main(){
//创建学生结构体数组
student stus[2]={
{"Tom",20,1002},
{"Mike",18,1003}
};
//输出元素的信息
cout<<stus[0].name<<"t"<<stus[0].age<<endl;
cout<<stus[1].name<<"t"<<stus[1].age<<endl;
return 0;
}
eg:
#include<iostream>
#include<string>
using namespace std;
struct emp{
string name;
int age;
double salary;
};
int main() {
emp emps[3];
for (int i = 0; i < 3; i++) {
string name;
int age;
double salary;
cout << "请输入第" << i + 1 << "个员工的姓名" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "个员工的年龄" << endl;
cin >> age;
cout << "请输入第" << i + 1 << "个员工的工资" << endl;
cin >> salary;
emp em = { name,age,salary };
emps[i] = em;
}
for (int i = 0; i < 3; i++) {
cout << "第" << i + 1 << "个员工的信息如下:" << endl;
cout << "姓名:" << emps[i].name << 't' << "年龄:" << emps[i].age << 't' << "薪资:" << emps[i].salary << endl;
}
return 0;
}
12.4 结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符
->
可以通过结构体指针访问结构体属性
1. 创建结构体变量
2. 通过指针指向结构体变量
3. 通过指针访问结构体变量中的数据
#include<iostream>
#include<string>
using namespace std;
struct student {
string name;
int age;
int score;
};
int main() {
struct student stu = { "张三",18,100 };
struct student* p = &stu;
cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
return 0;
}
12.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() {
teacher t;
t.id = 0002;
t.name = "老王";
t.age = 50;
t.stu.name = "小张";
t.stu.age = 20;
t.stu.score = 100;
cout << "老师姓名:" << t.name << " 老师年龄:" <<
t.age << " 老师教的学生:" << t.stu.name << endl;
return 0;
}
12.6 结构体做函数参数
作用:将结构体作为参数向函数中传递
传递的两种方式:
- 值传递 不改变主函数中的数据
- 地址传递 会改变主函数中的数据
#include<iostream>
#include<string>
using namespace std;
struct student {
string name;
int age;
int score;
};
//值传递
void printStudent01(struct student s) {
cout << "值传递" << endl;
cout << s.name << "," << s.age << "," << s.score << endl;
}
//地址传递
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本
//指针只占4个字节
void printStudent02(struct student *p) {
cout << "地址传递" << endl;
cout << p->name << "," << p->age << "," << p->score << endl;
}
int main() {
struct student stu;
stu.name = "张三";
stu.age = 20;
stu.score = 88;
printStudent01(stu);//值传递
printStudent02(&stu);//地址传递
return 0;
}
12.7 结构体中const
使用场景
作用:用const来防止误操作
#include<iostream>
#include<string>
using namespace std;
struct student {
string name;
int age;
int score;
};
//const使用场景 只能读数据,不能写数据
//const防止 在进行地址传递时,将主函数中的数据进行修改
void printStudent(const student* stu) {//加const防止函数体中的误操作
//stu->age=100;//操作失败,因为加了const修饰
cout << stu->name << "," << stu->age << "," << stu->score << endl;
}
int main() {
struct student stu = { "张三" ,20,88 };
printStudent(&stu);
return 0;
}
12.8 结构体案例
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student {
string name;
int score;
};
struct teacher {
string T_name;
struct student S_array[5];
};
//填写信息
void allocateSpace(struct teacher T_arrays[], int n,int m) {
string name = "abcde";
for (int i = 0; i < n; i++) {
T_arrays[i].T_name = "teacher_" ;
T_arrays[i].T_name += name[i];
for (int j = 0; j < m; j++) {
T_arrays[i].S_array[j].name = "student_";
T_arrays[i].S_array[j].name += name[j];
T_arrays[i].S_array[j].score = rand()%61+40;//40~100随机数
}
}
}
//打印信息
void printInfo(struct teacher T_arrays[], int n,int m) {
for (int i = 0; i < n; i++) {
cout<<"老师姓名:"<<T_arrays[i].T_name<<endl;
for (int j = 0; j < m; j++) {
cout<<"t学生姓名:"<< T_arrays[i].S_array[j].name<<"t考试分数:"<<
T_arrays[i].S_array[j].score<<endl;
}
}
}
int main() {
//随机数种子
srand((unsigned int)time(NULL));
struct teacher T_arrays[3];
int n = sizeof(T_arrays) / sizeof(T_arrays[0]);
int m = sizeof(T_arrays[0].S_array) / sizeof(T_arrays[0].S_array[0]);
allocateSpace(T_arrays, n, m);
printInfo(T_arrays, n, m);
return 0;
}
#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;
}
}
}
}
void printHero(struct hero h[], int len) {
for (int i = 0; i < len - 1; i++) {
cout << h[i].name << h[i].age << h[i].sex << endl;
}
}
int main() {
struct hero heroes[5] = {
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}
};
int len = sizeof(heroes) / sizeof(heroes[0]);
bubbleSort(heroes, len);
printHero(heroes, len);
return 0;
}
12.9 使用new创建动态结构体
将new
用于结构体由2步组成:
1)创建动态结构 2)访问其成员
创建动态结构体和使用new创建内置类型完全相同:
typeName *pointer_name = new typeName;
student *ps = new student;
访问动态结构体成员:(*ps).price3;
使用箭头运算符访问动态结构体成员:ps->name;
最后
以上就是含糊高跟鞋为你收集整理的【C++基础语法 12】 —— 结构体12 结构体的全部内容,希望文章能够帮你解决【C++基础语法 12】 —— 结构体12 结构体所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复