当前位置: 首页 > news >正文

结构体(个人学习笔记黑马学习)

1、结构体的定义和使用

#include <iostream>
using namespace std;
#include <string>struct Student {string name;int age;int score; 
}s3;int main() {//1、struct Student s1;s1.name = "张三";s1.age = 18;s1.score = 100;cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;//2、struct Student s2 = { "李四",19,80 };cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;//3、s3.name = "王五";s3.age = 20;s3.score = 60;cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;system("pause");return 0;
}

结构体变量创建的时候 struct可以省略


2、结构体数组

#include <iostream>
using namespace std;
#include <string>struct Student {string name;int age;int score; 
}s3;int main() {struct Student stuArray[3] = {{"张三",18,100},{"李四",28,99},{"王五",38,66}};stuArray[2].name = "赵六";stuArray[2].age = 80;stuArray[2].score = 60;for (int i = 0; i < 3; i++) {cout << stuArray[i].name <<" "<< stuArray[i].age <<" "<< stuArray[i].score << " "<<endl;}system("pause");return 0;
}


3、结构体指针

#include <iostream>
using namespace std;
#include <string>struct Student {string name;int age;int score; 
};int main() {struct Student s = { "张三",18,100 };struct Student* p = &s;cout << "姓名:" << p->name << " 年龄:" << p->age << " 成绩:" << p->score << endl;system("pause");return 0;
}

4、结构体嵌套结构体

#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.name = "小王";t.stu.age = 20;t.stu.score = 60;cout << "老师姓名:" << t.name << " 老师编号:" << t.id << " 老师年龄:" << t.age<< " 老师辅导的学生姓名:" << t.stu.name << " 学生年龄:" << t.stu.age << " 学生成绩:" << t.stu.score << endl;system("pause");return 0;
}

5、结构体做函数参数

#include <iostream>
using namespace std;
#include <string>struct student {string name;int age;int score; 
};void printfStudent1(struct student s) {cout << "在子函数中打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;}void printfStudent2(struct student* p) {cout << "子函数2中 姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}int main() {struct student s;s.name = "张三";s.age = 20;s.score = 85;//cout << "main函数中打印 姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;//printfStudent1(s);printfStudent2(&s);system("pause");return 0;
}


6、结构体中const的使用

#include <iostream>
using namespace std;
#include <string>struct student {string name;int age;int score; 
};void printfStudent(const student *s) {//s->age = 150;加入const之后,一旦有修改的操作就会报错,可以防止误操作cout << "姓名:" << s->name << " 年龄:" << s->age << " 分数:" << s->score << endl;
}int main() {struct student s;s.name = "张三";s.age = 15;s.score = 70;printfStudent(&s);system("pause");return 0;
}


7、案例一

案例描述:
学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员。

学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据。

#include <iostream>
using namespace std;
#include <string>
#include <ctime>struct student {string sName;int score;
};struct Teacher {string tName;struct student sArray[5];
};void allocatSpace(struct Teacher tArray[],int len) {string nameSeed = "ABCDE";for (int i = 0; i < len; i++) {tArray[i].tName = "Teacher_";tArray[i].tName += nameSeed[i];for (int j = 0; j < 5; j++) {tArray[i].sArray[j].sName = "Student_";tArray[i].sArray[j].sName += nameSeed[j];int random = rand() % 61+40;//40`100tArray[i].sArray[j].score = random;}}
}void printfInfo(struct Teacher tArray[], int len) {for (int i = 0; i < len; i++) {cout << "老师的姓名:" << tArray[i].tName << endl;for (int j = 0; j < len; j++) {cout << "\t学生的姓名:" << tArray[i].sArray[j].sName <<" 考试分数:" << tArray[i].sArray[j].score << endl;}}
}int main() {srand((unsigned int)time(NULL));Teacher tArray[3];int len = sizeof(tArray) / sizeof(tArray[0]);allocatSpace(tArray, len);printfInfo(tArray,len);system("pause");return 0;
}


8、案例二

案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。
通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。

#include <iostream>
using namespace std;
#include <string>struct Hero {string name;int age;string sex;
};void bubbleSort(struct Hero heroArray[], int len) {for (int i = 0; i < len - 1; i++) {for (int j = 0; j < len - i - 1; j++) {if (heroArray[j].age > heroArray[j + 1].age) {struct Hero temp = heroArray[j];heroArray[j] = heroArray[j + 1];heroArray[j + 1] = temp;}}}
}void printfHero(struct Hero heroArray[], int len) {for (int i= 0; i < len; i++) {cout << "姓名:" << heroArray[i].name << " 年龄:" << heroArray[i].age<< " 性别:" << heroArray[i].sex << endl;}
}int main() {struct Hero heroArray[5] ={{"刘备",23,"男"},{"关羽",22,"男"},{"张飞",20,"男"},{"赵云",21,"男"},{"貂蝉",19,"女"},};int len = sizeof(heroArray) / sizeof(heroArray[0]);bubbleSort(heroArray, len);printfHero(heroArray, len);system("pause");return 0;
}

http://www.lryc.cn/news/147638.html

相关文章:

  • 小白带你学习linux的PXE装机
  • 华为鲲鹏服务器
  • Python金币小游戏
  • Modbus转Profinet网关在大型自动化仓储项目应用案例
  • Java 并发 ThreadLocal 详解
  • JWT 技术的使用
  • 机器学习深度学习——NLP实战(自然语言推断——微调BERT实现)
  • 如何在windows下使用masm和link对汇编文件进行编译
  • Golang字符串基本处理方法
  • 算法训练营第三十九天(8.30)| 动态规划Part09:购买股票
  • renren-fast-vue环境升级后,运行正常打包后,访问页面空白
  • Uniapp笔记(三)uniapp语法2
  • windows【ftp-FTP】添加配置流程【iis服务】
  • mysql视图的创建和选项配置详解
  • Python正则表达式中re.sub自定义替换方法正确使用方法
  • hyperf 十五 验证器
  • ssh访问远程宿主机的VMWare中NAT模式下的虚拟机
  • 【一等奖方案】大规模金融图数据中异常风险行为模式挖掘赛题「NUFE」解题思路
  • npm install 报错
  • 专业人士使用的3个好用的ChatGPT提示
  • doris系列2: doris分析英国房产数据集
  • 精准运营,智能决策!解锁天翼物联水利水务感知云
  • CleanMyMac最新版4.14Mac清理软件下载安装使用教程
  • String.Format方法详解
  • 【Mysql】关联查询1对多处理
  • vue 入门案例模版
  • el-select实现懒加载
  • Java泛型机制
  • Linux CentOS安装抓包解包工具Wireshark图形化界面
  • 虹科分享 | 温度边缘效应对冻干成品含水量的影响(下)——优化和总结