2021-11-05 C++结构体方面的知识
C++ 结构体方面的知识
1 基础语法
1.1 创建结构体变量,三种方法
#include<iostream>
using namespace std;
//1. 创建学生数据类型
//自定义数据类型,一些类型集合组成的一个类型
struct student
{
//学生类型
string name;
int age;
int score;
};
int main()
{
//2. 通过学生类型创建具体学生
// 2.1
// struct 关键字可以在创建变量的时候省略
struct student s1;
s1.age = 19;
s1.name = "穆久涛";
s1.score = 100;
cout << "姓名" << s1.name << "年龄"
<< s1.age << "分数" << s1.score<< endl;
// 2,2 struct student s1 ={...}
struct student s2 = { "穆久涛", 19, 80 };
cout << "姓名" << s1.name << "年龄"
<< s1.age << "分数" << s1.score << endl;
// 2.3
// 在定义结构体时候创建结构体变量
struct student
{
//学生类型
string name;
int age;
int score;
}s3;
s3.name = "礼拜";
s3.age = 11;
s3.score = 22;
return 0;
system("pause");
}
1.2 修改结构体数组内容,遍历内容
#include<iostream>
using namespace std;
//1. 定义结构体
struct student
{
//学生类型
string name;
int age;
int score;
};
int main()
{
//2. 创建结构体数组
struct student stuarray[3] =
{
{"穆久涛",19, 99},
{"李四",22, 89},
{"王五",19, 77},
};
//3. 给结构体数组中的元素赋值, 修改其中的值
stuarray[2].age = 44;
stuarray[2].name = "别杀我";
stuarray[2].score = 44;
//4. 遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << "姓名" << stuarray[i].name
<< "年龄" << stuarray[i].age
<< "分数" << stuarray[i].score << endl;
}
return 0;
system("pause");
}
1.3 结构体指针的用法
#include<iostream>
using namespace std;
// 利用操作符 -> 访问结构体属性
//1. 定义结构体
struct student
{
//学生类型
string name;
int age;
int score;
};
int main()
{
//2. 创建结构体数组
struct student stuarray = { "穆久涛",19, 99 };
// 通过指针指向结构体变量
student * p = &stuarray; // auto = student
// 通过指针访问结构体变量中的数据
cout << p->name << endl;
//通过结构体指针访问结构体中的变量
return 0;
system("pause");
}
1.4 嵌套结构体的使用
#include<iostream>
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 = 1000;
t.age = 40;
t.name = "老王";
t.stu.age = 18;
t.stu.name = "小王";
t.stu.score = 99;
cout << t.name << t.age << t.stu.name << endl;
return 0;
system("pause");
}
1.5 结构体作函数参数
#include<iostream>
using namespace std;
//定义学生的结构体
struct student
{
//学生类型
string name;
int age;
int score;
};
//打印学生信息函数
//1. 值传递
void printstudent1(student s)
{
s.age = 999; //只会在函数中修改
cout << "姓名" << s.name << "年龄"
<< s.age << "分数" << s.score << endl;
}
//2. 地址传递
void printstudent2(student *s)
{
cout << "姓名" << s->name << "年龄"
<< s->age << "分数" << s->score << endl;
}
int main()
{
//将结构体作为函数参数
//将学生传入到一个参数中,打印学生身上的所有图片
//创建结构体变量
struct student s;
s.score = 100;
s.age = 40;
s.name = "老王";
printstudent1(s);
printstudent2(&s);
cout << "姓名" << s.name << "年龄"
<< s.age << "分数" << s.score << endl;
return 0;
system("pause");
}
1.6 结构体中const的使用场景
#include<iostream>
using namespace std;
//定义学生的结构体
struct student
{
//学生类型
string name;
int age;
int score;
};
//打印学生信息函数
//1. 地址传递; 不会增加内存信息good!!!!!!
void printstudent1(const struct student *s) //const !!!!!!!!!!!!!
{
// s->age = 999; //只会在函数中修改 // 加上const之后就不能修改了
cout << "姓名" << s.name << "年龄"
<< s.age << "分数" << s.score << endl;
}
int main()
{
//将结构体作为函数参数
//将学生传入到一个参数中,打印学生身上的所有图片
//创建结构体变量
struct student s;
s.score = 100;
s.age = 40;
s.name = "老王";
printstudent1(&s);
return 0;
system("pause");
}
2 三个结构体相关的案例
2.1 三个老师带着五个学生的案例
#include<iostream>
using namespace std;
struct student
{
string sname;
int age;
int score;
};
//定义老师的结构体
struct teacher
{
//老师类型
string tname;
struct student stu[5];
};
void allocatespace(struct teacher tea[3],int len)
{
string Q = "ABCDE";
for (int i = 0; i < 3; i++)
{
tea[i].tname = "teacher_";
tea[i].tname += Q[i];
for (int j = 0; j < 5; j++)
{
tea[i].stu[j].score = rand() % 61 + 40;
tea[i].stu[j].age = rand() % 21;
tea[i].stu[j].sname = "student_";
tea[i].stu[j].sname += Q[j];
}
}
}
void printarray(struct teacher tea[])
{
for (int i = 0; i < 3; i++)
{
cout << "老师姓名为:" << tea[i].tname << "这个老师带的学生为:" << endl;
for (int j = 0; j < 5; j++)
{
cout << "学生姓名为 :" << tea[i].stu[j].sname
<< " 学生年龄为:" << tea[i].stu[j].age
<< " 学生成绩为:" << tea[i].stu[j].score << endl;
}
}
}
int main()
{
//1.定义结构体
struct teacher tea[3];
//2.填入数据
int len = 3;
allocatespace(tea, len);
//3. 输出数据
printarray(tea);
return 0;
system("pause");
}
2.2 对结构体数组进行比较,冒泡排序
#include<iostream>
using namespace std;
//定义英雄的结构体
struct hero
{
//英雄类型
string tname;
int age;
string sex;
};
void compare(struct hero h[])
{
for (int i = 0; i < 5-1; i++)
{
for (int j = 0; j < 5-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 printarray(struct hero h[5])
{
for (int i = 0; i < 5; i++)
{
cout << "英雄姓名:" << h[i].tname
<< " 英雄年龄:" << h[i].age
<< " 英雄性别: " << h[i].sex << endl;
}
}
int main()
{
//1.定义结构体
struct hero h[5] =
{
{"刘备",23,"男"},
{"张飞",27,"男"},
{"相遇",33,"男"},
{"赵云",43,"男"},
{"李白",13,"男"},
};
//3. 进行比较
compare(h);
//2. 输出数据
printarray(h);
return 0;
system("pause");
}
2.3 通讯录管理系统
通讯录是一个可以记录亲人、好友信息的工具。
·1添加联系人︰向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人·
·2显示联系人:显示通讯录中所有联系人信息
·3删除联系人:按照姓名进行删除指定联系人
·4查找联系人:按照姓名查看指定联系人信息
·5修改联系人:按照姓名重新修改指定联系人
·6清空联系人:清空通讯录中所有信息
·0退出通讯录:退出当前使用的通讯录
#include<iostream>
using namespace std;
#define MAX 1000
void showmenu()
{
cout << "**********************" << endl;
cout << "*****1.添加联系人*****" << endl;
cout << "*****2.显示联系人*****" << endl;
cout << "*****3.删除联系人*****" << endl;
cout << "*****4.查找联系人*****" << endl;
cout << "*****5.修改联系人*****" << endl;
cout << "*****6.清空联系人*****" << endl;
cout << "*****0.退出通讯录*****" << endl;
}
struct person
{
string m_name;
int m_sex;
int m_age;
string m_phone;
};
struct addressperson
{
struct person personarray[MAX];
int m_size;
};
// 1 添加联系人
void addperson(struct addressperson *abs)
{
if (abs->m_size == MAX)
{
cout<<"达到最大值,别再输入了" << endl;
}
else
{
//输入姓名
cout<<"请输入姓名" << endl;
string name;
cin >> name;
abs->personarray[abs->m_size].m_name = name; %% abs是结构体指针,他只会指向它内部的m_size和personarray!!
cout << "请输入性别" << endl;
cout << "1--男人" << endl;
cout << "2--女人" << endl;
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[abs->m_size].m_sex = sex;
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->personarray[abs->m_size].m_age = age;
cout << "请输入电话" << endl;
int phone;
cin >> phone;
abs->personarray[abs->m_size].m_phone = phone;
//更新人数
abs->m_size++;
system("pause");
system("cls");
}
}
// 2 显示联系人
void showperson(struct addressperson* abs)
{
if (abs->m_size == 0)
{
cout << "通讯录一个人也没有" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout <<"姓名:" << abs->personarray[i].m_name << "\t";
if (abs->personarray[i].m_sex == 1)
{
cout<<"性别:男" << endl;
}
else
{
cout<<"性别:女" << endl;
}
cout << "年龄:" << abs->personarray[i].m_age << "\t";
cout << "电话:" << abs->personarray[i].m_phone << endl;
}
system("pause");
system("cls");
}
}
// 3 删除联系人
int isexist(struct addressperson* abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personarray[i].m_name == name)
{
return i;
}
}
return -1;
}
void deleteperson(struct addressperson* abs)
{
cout<<"请输入你要删除的联系人" << endl;
string name;
cin >> name;
int ret = isexist( abs, name);
if (ret != -1)
{
//找到人了,进行删除操作
for (int i = ret; i < abs->m_size; i++)
{
//数据前移,全部都前移
abs->personarray[i] = abs->personarray[i + 1];
}
abs->m_size--; //更新通讯录的人数
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 4. 查找联系人
void findperson(struct addressperson* abs)
{
cout << "请输入你要查找的联系人的姓名" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret != -1)
{
cout << "姓名" << abs->personarray[ret].m_name << "\t";
cout << "性别" << abs->personarray[ret].m_sex << "\t";
cout << "年龄" << abs->personarray[ret].m_age << "\t";
cout << "电话" << abs->personarray[ret].m_phone << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 5 修改联系人
void modifyperson(struct addressperson* abs)
{
cout << "请输入你要修改的联系人的姓名" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret != -1)
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
abs->personarray[ret].m_name = name;
cout << "请输入性别" << endl;
cout << "1--男人" << endl;
cout << "2--女人" << endl;
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[ret].m_sex = sex;
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->personarray[ret].m_age = age;
cout << "请输入电话" << endl;
int phone;
cin >> phone;
abs->personarray[ret].m_phone = phone;
cout << "录入完毕" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void cleanperson(struct addressperson* abs)
{
abs->m_size = 0;
cout<<"已经清空了" << endl;
system("pause");
system("clc");
}
int main() {
struct addressperson abs;
abs.m_size = 0;
while (true)
{
showmenu();
cout << "请输入选项" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1:
addperson(&abs);
break;
case 2:
showperson(&abs);
break;
case 3:
deleteperson(&abs);
break;
case 4:
findperson(&abs);
break;
case 5:
addperson(&abs);
break;
case 6:
cleanperson(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
}