结构体
结构体指针
作用通过指针访问结构体中的成员 利用"->"可以通过结构体指针来访问结构体属性
#include <iostream>
using namespace std;
#include <string>
//结构体指针
//定义学生结构体
struct Student
{
string name; //姓名
int age; //年龄
int score; //分数
};
int main()
{
//1、创建学生结构体变量
//struct Student s = {"张三", 20, 90};
Student s = {"张三", 20, 90};
//2、通过指针指向结构体变量
//struct Student * p = &s;
Student * p = &s;
//3、通过指针访问结构体变量中的数据
//通过结构体指针访问结构体变量中的数据,使用 -> 运算符
//s.name;
cout << "姓名:" << p->name << endl; //通过指针访问结构体变量中的数据
cout << "年龄:" << p->age << endl;
cout << "分数:" << p->score << endl;
system("pause");
return 0;
}
结构体指针通过->来访问结构体内的数据
结构体嵌套结构体
作用结构体中的成员可以是另一个结构体 例如每一个老师辅导一个学生,一个老师的结构体中,记录一个学生的结构体
#include <iostream>
using namespace std;
#include <string>
//定义学生结构体
struct student
{
int id; //学生编号
string name; //学生姓名
int age; //学生年龄
};
//定义老师结构体
struct teacher
{
int id; //教师编号
string name; //教师姓名
int age; //教师年龄
struct student stu; //教师所带的学生
};
int main()
{
//结构体嵌套结构体
//创建老师
struct teacher t1;
t1.id = 1001;
t1.name = "张老师";
t1.age = 30;
t1.stu.id = 2001;
t1.stu.name = "李华";
t1.stu.age = 18;
cout << "教师编号:" << t1.id << endl;
cout << "教师姓名:" << t1.name << endl;
cout << "教师年龄:" << t1.age << endl;
cout << "学生编号:" << t1.stu.id << endl;
cout << "学生姓名:" << t1.stu.name << endl;
cout << "学生年龄:" << t1.stu.age << endl;
system("pause");
return 0;
}
结构体做函数参数
作用将结构体作为参数向函数中传递 传递的方式有两种: 1、值传递 2、地址传递
#include <iostream>
using namespace std;
#include <string>
//定义学生结构体
struct student
{
int id; //学生编号
string name; //学生姓名
int age; //学生年龄
};
//打印学生信息的函数
//1、值传递
void printStudent1(student s)
{
s.id = 1001;
cout << "值传递学生编号:" << s.id << endl;
cout << "值传递学生姓名:" << s.name << endl;
cout << "值传递学生年龄:" << s.age << endl;
}
//2、地址传递
void printStudent2(student* s)
{
s->age = 80;
cout << "地址传递学生编号:" << s->id << endl;
cout << "地址传递学生姓名:" << s->name << endl;
cout << "地址传递学生年龄:" << s->age << endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上的所有信息
//创建结构体变量
struct student s1;
s1.id = 2001;
s1.name = "李华";
s1.age = 18;
cout << "学生编号:" << s1.id << endl;
cout << "学生姓名:" << s1.name << endl;
cout << "学生年龄:" << s1.age << endl;
//调用函数
printStudent1(s1);
printStudent2(&s1);
printStudent1(s1);
system("pause");
return 0;
}
总结如果不想修改主函数中的数据,用值传递,反之用地址传递
结构体中const使用场景
作用用const来放置误操作
#include <iostream>
using namespace std;
#include <string>;
//const的使用场景
struct student
{
int id;
string name;
int age;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
//坏处就是里面改了,外部的也会改掉
void printStudent(const struct student *s)
{
//加完就不可以修改了信息了,只能读取,如果有修改就会报错,防止误操作
//(s->id)++;
//(s->age)++;
cout << "学号: " << s->id << endl;
cout << "姓名: " << s->name << endl;
cout << "年龄: " << s->age << endl;
}
int main()
{
//创建结构体变量
struct student s1 = {1001, "张三", 20};
//通过函数打印学生信息
printStudent(&s1);
system("pause");
return 0;
}
结构体案例1
案例1 学校正在做毕设,每名老师带5名学生,总共3个老师,需求如下: 设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5个学生的数组作为成员 学生的成员有姓名,考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值。 最终打印老师数据和所带的学生的数据
#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
#include <time.h>
//创建学生结构体
struct student
{
string name; //姓名
double score; //成绩
};
//创建老师结构体
struct teacher
{
string name; //姓名
struct student stu[5]; //学生数组
};
//给老师和学生赋值的函数
void allocateSpace(teacher t[],int len)
{
string nameSeed = "ABCDE";
//给老师赋值
for (int i = 0; i < len; i++)
{
//t[i].name = "老师" + nameSeed[i];
//错的,你把「字符串字面量」和「char 字符」直接用 + 号拼接了,C++ 不允许这么写!
//C++ 会把它们当成地址 + 数字,直接算指针偏移,结果变成野指针,最终崩溃。
//正确的写法是先把「字符串字面量」转换成 string 对象,然后再用 + 号拼接:
t[i].name = "老师";
t[i].name += nameSeed[i]; //老师A老师
//给学生赋值
for (int j = 0; j < 5; j++)
{
//t[i].stu[j].name = "学生" + nameSeed[j];
t[i].stu[j].name = "学生";
t[i].stu[j].name += nameSeed[j];
//生成随机成绩
int arndom = rand() % 61 + 40; //生成40-100的随机数
t[i].stu[j].score = arndom;
}
}
}
//打印所有人的信息
void printfinfo(teacher t[],int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师姓名: " << t[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t学生姓名: " << t[i].stu[j].name //\t会舒服一个
<< "\t成绩: " << t[i].stu[j].score << endl;
}
}
}
int main()
{
//设置随机种子种子
srand((unsigned int)time(NULL));
//1、创建3名老师的数组
teacher t[3];
//2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
int len = sizeof(t)/sizeof(t[0]);
allocateSpace(t,len);
//3、打印所有老师及所带的学生的信息
printfinfo(t,len);
return 0;
}
案例2
设计一个英雄的结构体,包括成员姓名,年龄,性别,创建结构体数组,数组中存放5名英雄 通过冒泡排序算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
#include <iostream>
using namespace std;
#include <string>
//1、设计英雄结构体
struct hero
{
string name; //姓名
int age; //年龄
string sex; //性别
};
//2、设计冒泡排序函数
void bubbleSort(hero h[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
//比较相邻两个元素的年龄
if (h[j].age > h[j + 1].age)
{
//交换
hero temp = h[j];
h[j] = h[j + 1];
h[j + 1] = temp;
}
}
}
}
//打印输出
void printHero(hero h[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "姓名: " << h[i].name << endl;
cout << "年龄: " << h[i].age << endl;
cout << "性别: " << h[i].sex << endl;
}
}
int main()
{
//2、创建数组存放五名英雄
hero h[5] = {
{"刘备", 23, "男"},
{"关羽", 22, "男"},
{"张飞", 21, "男"},
{"赵云", 20, "男"},
{"貂蝉", 19, "女"}
};
int len = sizeof(h) / sizeof(h[0]);
//for (int i = 0; i < len; i++)
//{
// cout << "姓名: " << h[i].name << endl;
// cout << "年龄: " << h[i].age << endl;
// cout << "性别: " << h[i].sex << endl;
// }
//3、对数组进行排序,按照年龄进行升序排序
bubbleSort(h, len);
//4、将结果排序后打印输出
printHero(h, len);
return 0;
}
内存分区模型
c++程序在执行时,将内存大方向分为4个区域 1、代码区:存放函数体的二进制代码,由操作系统进行管理 2、全局区:存放全局变量和静态变量以及常量的 3、栈区:由编译器自动分配释放,存放函数的参数值,局部变量等 4、堆区:由程序员分配和释放,若程序员不释放,程序结束后,有操作系统回收
内存四区意义不同区域存放的数据,赋予不同的生命周期,给我们更大的灵活编程。
代码区
1.1程序运行前
在程序编译后,生成了exe可执行程序,未执行程序前分为两个区域 代码区 1、存放CPU执行的机器指令 2、代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一段代码就可以了 3、代码区是只读的,使其只读的原因是防止程序意外的修改了他的指令。
全局区
1.1程序运行前
全局区 1、全局变量和静态变量存放在此 2、全局区还包含了常量区,字符串常量和其他常量(const)也存放在此 3、该区域的数据在程序结束后由操作系统释放
#include <iostream>
using namespace std;
//全局变量
int g_a = 100;
int g_b = 200;
//const修饰的全局变量
const int g_c = 300;
int main()
{
//全局区
//全局变量、静态变量,常量
//创建一个普通局部变量
int a = 10;
int b = 20;
cout << "局部变量a地址" << (long long)&a << endl;
cout << "局部变量b地址" << (long long)&b << endl;
cout << "全局变量g_a地址" << (long long)&g_a << endl;
cout << "全局变量g_b地址" << (long long)&g_b << endl;
//静态变量 在普通变量前面加static关键字修饰的变量就是静态变量
static int s_a = 1000;
static int s_b = 2000;
cout << "静态变量s_a地址" << (long long)&s_a << endl;
cout << "静态变量s_b地址" << (long long)&s_b << endl;
//常量
//字符串常量
cout << "字符串常量地址" << (long long)&"hello world" << endl;
//const修饰的常量
//const修饰的全局常量
cout << "const修饰的全局常量g_c地址" << (long long)&g_c << endl;
//const修饰的局部常量
const int c_l_a = 400;
cout << "const修饰的局部常量c_l_a地址" << (long long)&c_l_a << endl;
return 0;
}
总结 1、 c++中在程序运行前分为全局区和代码区 2、代码区特点是共享和只读 3、全局区存放全局变量,静态变量,常量 4、常量中存放const修饰的全局常量和字符串常量