问题:地址传递一定比值传递节约内存吗?
不一定。 当参数传入到函数中时,函数会在栈空间中,为函数参数开辟对应的内存。 如果传入的是指针,那么系统会为该参数分配指针类型大小的空间,比如32位机就是4个字节。 而传入的变量,分配空间大小就是实际占用的大小。比如char就是1个字节,short就是2个字节,double就是8个字节等。 可以看到,指针和
结构体中const的使用场景
用const防止误操作
在main方法中使用结构体变量为参数的函数时,传入结构体变量,就是值传递,他会把结构体中所有属性都复制一份存入内存,如果结构体属性很多,那么占据内存就多,如果main方法中定义的是结构体数组的话,占据的内存更多。这时将函数参数设置成指针,也就是地址传递的话,结构体无论有多少个属性,都只会占据一个指针4字节的空间(32位),某种程度上会起到节省空间的作用
但是 如果在函数中有操作结构体属性的语句,就容易直接把实参修改,防止误操作引入const
//将函数形参改为指针,可以减少内存空间,而且不会复制新的副本 void printStudent(const Student* stu) { //stu->age = 150;修改属性会报错 cout << "地址传递:" << stu->name << endl; cout << "地址传递:" << stu->age << endl; cout << "地址传递:" << stu->score << endl; } int main() { Student stu1 = { "张三",20,100 }; printStudent(&stu1); system("pause"); return 0; }
结构体案例1
#include"methodState.h" struct Student { string sname; int score; }; struct Teacher { string tname; Student sarrays[5]; }; void tea_Stu(Teacher tarrays[],int len) { //这里可以改成void tea_Stu(Teacher *tarrays,int len) string name = "ABCDE"; string name1 = "12345"; for (int i = 0; i < len; i++) { tarrays[i].tname = "Teacher_"; tarrays[i].tname += name[i]; for (int j = 0; j < 5; j++) { tarrays[i].sarrays[j].sname = tarrays[i].tname + "_Student_"+ name1[j]; tarrays[i].sarrays[j].score = 10 + j; } } for (int i = 0; i < len; i++) { cout << "老师姓名:" << tarrays[i].tname << endl; cout << "老师带的学生名字与成绩:" << endl; for (int j = 0; j < 5; j++) { cout << tarrays[i].sarrays[j].sname << " " << tarrays[i].sarrays[j].score << endl; } } } int main() { Teacher tarrays[3]; int len = sizeof(tarrays) / sizeof(tarrays[0]); tea_Stu(tarrays,3); system("pause"); return 0; }
输入信息可以用cin优化
void tea_Stu1(Teacher tarrays[], int len) { for (int i = 0; i < len; i++) { cout << "请输入第" << i+1 << "位老师的姓名:"; cin >> tarrays[i].tname; cout << endl; for (int j = 0; j < 5; j++) { cout << "请输入他的第" << j+1 << "名学生的姓名:"; cin>>tarrays[i].sarrays[j].sname; cout << endl; cout << "请输入该名学生的成绩:"; cin >> tarrays[i].sarrays[j].score; } } for (int i = 0; i < len; i++) { cout << "老师姓名:" << tarrays[i].tname << endl; cout << "老师带的学生名字与成绩:" << endl; for (int j = 0; j < 5; j++) { cout << tarrays[i].sarrays[j].sname << " " << tarrays[i].sarrays[j].score << endl; } } }
也可以生成随机数成绩,之前讲过
srand((unsigned int)time(NULL)); tarrays[i].sarrays[j].score =rand()%60;//添加随机数种子并生成随机数0-59
结构体案例2
#include"methodState.h" #include<cstdlib> struct Legend { string name; int age; string sex; }; void BubbleSort(Legend arrays[], int len) { for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - 1 - i; j++) { if (arrays[j].age > arrays[j + 1].age) { Legend index = arrays[j]; arrays[j] = arrays[j + 1]; arrays[j + 1] = index; } } } for (int i = 0; i < len; i++) { cout << arrays[i].name << " " << arrays[i].age << " " << arrays[i].sex << endl; } } int main() { Legend arrays[5] = { {"孙悟空",8000,"男"}, {"猪八戒",3000,"男"}, {"唐僧",30,"男"}, {"白龙马",500,"男"}, {"沙悟净",800,"男"} }; int len = sizeof(arrays) / sizeof(arrays[0]); BubbleSort(arrays, len); system("pause"); return 0; }
案例:通讯录管理系统
样图
暂时先写到增加和显示
#include <iostream> using namespace std; #define Max 1000 struct Person { string name; string sex; int age; long long number; string address; }; struct addressBook { Person arrays[Max]; int size; }; void insert(addressBook* ads) {//增 //因为需要确实的加一个人进去,所以需要用地址传递(值传递只改形参) if (ads->size == Max) { cout << "通讯录满" << endl; return; } else { cout << "请输入新联系人的姓名:"; cin >> ads->arrays[ads->size].name; cout << endl; cout << "请输入新联系人的性别:"; cin >> ads->arrays[ads->size].sex; cout << endl; cout << "请输入新联系人的年龄:"; cin >> ads->arrays[ads->size].age; cout << endl; cout << "请输入新联系人的电话:"; cin >> ads->arrays[ads->size].number; cout << endl; cout << "请输入新联系人的地址:"; cin >> ads->arrays[ads->size].address; cout << endl; cout << "添加成功" << endl; ads->size++; system("pause"); return; } } void select(addressBook* ads) {//显示 if (ads->size == 0) { cout << "通讯录为空" << endl; cout << "按任意键返回" << endl; system("pause"); return; } else { cout << "姓名 性别 年龄 电话号码 家庭住址"; cout << endl; for (int i = 0; i < ads->size; i++) { cout << ads->arrays[i].name << " " << ads->arrays[i].sex << " " << ads->arrays[i].age << " " << ads->arrays[i].number << " " << ads->arrays[i].address << endl; } system("pause"); return; } } void del() {}//删 void update() {}//改 void search() {}//搜索 void destory() {}//清空 int main() { addressBook ads; ads.size = 0; while(true){ cout << "Welcome to Addressbook Management System" << endl; cout << "1. 新建联系人" << endl; cout << "2. 显示联系人" << endl; cout << "3. 删除联系人" << endl; cout << "4. 修改联系人" << endl; cout << "5. 搜索联系人" << endl; cout << "6. 清空联系人" << endl; cout << "0. 退出系统" << endl; cout << "请输入您的选择" << endl; int a; cin >> a; switch (a) { case 1:insert(&ads); break; case 2:select(&ads); break; case 3:del(); break; case 4:update(); break; case 5:search(); break; case 6:destory(); break; case 0: { cout << "欢迎下次使用" << endl; return 0; break; } default:break; } } system("pause"); return 0; }
P79结束
来源:b站黑马