C++语言程序设计——10 结构体
一、结构体的定义
结构体是 C++ 中一种可用户自定义的数据类型,可以将不同类型的数据组合在一起,形成一个新的复合数据类型。
可以把结构体理解成把一些重要数据进行打包,可以使其有多个属性,例如学生结构体,里面有姓名、性别、年龄等。
结构体(Struct)由以下三个部分组成,分别是关键字、结构体名称和成员列表。
struct 结构体名称
{
数据类型1 成员变量1;
数据类型2 成员变量2;
// ...
}结构体变量;
另外,在 C++ 结构体中,可以跳过 typedef 用于给结构体类型创建一个别名(新名字),这样可以简化代码并提高可读性。
typedef struct student // 使用typedef创建别名
{
string name;
int id;
double score;
} s; // s 是 struct student 的别名
使用之后,可以直接使用别名:
typedef struct student
{
string name;
int id;
double score;
} s;
s e1; // 不需要写struct关键字,直接使用别名
s e2;
不过一般来说,最简单还是通过结构体变量来实现。
二、结构体变量
结构体变量,如果不加,只是定义了结构体类型,没有自动创建变量,如果需要使用,必须单独声明变量。
结构体通过使用英文点号 . 来获取单个成员,从而获取成员的值,也可以给成员赋值。
结构体变量名.成员名
或者也可以间接访问(指针):用 -> 运算符。
指针名 -> 成员名
#include <iostream>
#include <string>
using namespace std;
// 结构体:默认访问权限是 public
struct Student
{
int id; // 默认 public,外部可直接访问
string name; // 默认 public
double score; // 默认 public
void showInfo() // 默认 public,外部可直接调用
{
cout << "学号:" << id << ",姓名:" << name << ",分数:" << score << endl;
}
};
int main()
{
Student s;
s.id = 1000;
s.name = "张三";
s.score = 95.5;
cout << "姓名:" << s.name << endl;
s.showInfo();
// 指针访问:定义指针指向对象 s(核心:& 取对象地址)
Student* p = &s;
// 指针读取成员变量:指针名 -> 成员变量
cout << "指针访问 - 原始学号:" << p->id << endl;
cout << "指针访问 - 原始分数:" << p->score << endl;
// 指针修改成员变量:指针名 -> 成员变量 = 新值
p->id = 1001; // 用 -> 修改学号
p->score = 98.0; // 用 -> 修改分数
// 指针调用成员函数:指针名 -> 成员函数()
cout << "指针修改后:" << endl;
p->showInfo(); // 指针调用 showInfo()
return 0;
}
例如,下面是一个结构体,在定义结构体类型的同时创建了结构体变量 a ,所以主函数里可以直接使用,比如 a.age:
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
char name[20];
int age;
int id;
char sex[10];
} a; // 声明 a 这个结构体变量
int main()
{
strcpy(a.name, "张三"); // 直接使用变量a
a.age = 20;
cout << a.name << " " << a.age << endl;
return 0;
}
运行结果如下:
而如果我们不创建变量,在主函数中需要单独声明变量。例如 student a,即单独声明变量 a(先定义结构体,再单独声明变量,可以省略 struct):
#include <iostream>
#include <cstring>
struct student
{
char name[20];
int age;
int id;
char sex[10];
}; // 只定义结构体类型,不创建变量
int main()
{
student a; // 单独声明变量a
strcpy(a.name, "李四");
a.age = 21;
cout << a.name << " " << a.age << endl;
return 0;
}
另外,我们定义变量时可以直接初始化成员,例如,student stu1 = {“张三”, 20, 1001, “男”},直接初始化 stu1 结构体成员:
#include <iostream>
using namespace std;
struct student
{
char name[20];
int age;
int id;
char sex[10];
}; // 只定义结构体类型,不创建变量
int main()
{
student stu1 = {"张三", 20, 1001, "男"}; // 声明并初始化结构体变量
cout << stu1.name << endl;
stu1.age = 21; // 访问和修改成员
cout << stu1.age << endl;
cout << stu1.id << endl;
cout << stu1.sex << endl;
}
运行结果如下:
例如,我们需要存储很多学生的相关信息,需要输入和输出,可以先创建一个 student 结构体,然后创建一个结构体数组来存储学生,同时输入和输出学生信息,通过这个结构体数组,可以很方便地使用索引(如arr[0]、arr[1])来访问和操作不同学生的信息:
#include <iostream>
using namespace std;
struct student
{
int id;
char name[10];
int score;
};
int main()
{
student arr[3]; // 结构体数组,存储多个学生
for (int i = 0; i < 3; i++) // 输入多个学生信息
cin >> arr[i].id >> arr[i].name >> arr[i].score;
for (int i = 0; i < 3; i++) // 输出所有学生信息
{
cout << "学号:" << arr[i].id << endl;
cout << "姓名:" << arr[i].name << endl;
cout << "分数:" << arr[i].score << endl;
}
return 0;
}
运行结果如下:
三、结构体数组
例如,下面代码就定义了一个结构体数组,通过定义结构体数组存储学生信息,并输出前两名学生的学号和分数:
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
int id;
char name[30];
float score;
}st[3] = { {1001,"HB",99},{1002,"SP",100},{1003,"SL",90} }; // 同时定义结构体数组st[3],并初始化3个学生的数据
int main()
{
cout << st[0].id << " " << st[0].score << endl; // 输出第1个学生(st[0])的学号和分数
cout << st[1].id << " " << st[1].score << endl;
return 0;
}
结构体应用例题1
例如,我们要对图书馆书籍进行整理,方便以后查询时候,根据作者迅速找出相应的书籍信息,就可以使用结构体来实现代码。
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
int id;
char name[100];
char author[50];
};
int main()
{
int n;
cin>>n;
student s[100];
for (int i = 0; i < n; i++)
cin >> s[i].id >> s[i].name >> s[i].author;
for (int i = 0; i < n; i++)
{
if (strcmp(s[i].author, "鲁迅") == 0) // 使用strcmp比较字符串,返回0表示两个字符串相等
cout << s[i].id <<" " << s[i].name <<" " << s[i].author<<endl;
}
return 0;
}
我们也可以更改一下代码, 使用 C++ 的 string 类(需要包含 < string > 头文件),直接使用 == 来比较字符串内容:
#include <iostream>
#include <string>
using namespace std;
struct student
{
int id;
string name;
string author;
};
int main()
{
int n;
cin >> n;
student s[100];
for (int i = 0; i < n; i++)
cin >> s[i].id >> s[i].name >> s[i].author;
cout << "-------------------------"<< endl;
for (int i = 0; i < n; i++)
{
if (s[i].author == "鲁迅")
cout << s[i].id << " " << s[i].name << " " << s[i].author << endl;
}
return 0;
}
运行结果如下:
结构体应用例题2
下面代码是实现了读取若干种饮料的信息(名称、价格、第三个属性),按以下规则排序后输出(使用了冒泡排序):
1、先按价格升序,价格低的在前;
2、价格相同则按名称长度升序,名称短的在前;
3、价格和名称长度都相同则按第三个属性升序。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct st
{
string a;
int b;
int c;
};
int main()
{
int n;
cin >> n;
st s[10000]; // 存储饮料信息
for (int i = 0; i < n; i++)
cin >> s[i].a >> s[i].b >> s[i].c;
for (int i = 0; i < n - 1; i++) // 冒泡排序实现多条件排序
{
for (int j = 0; j < n - 1 - i; j++)
{
bool needswap=false;
if (s[j].b > s[j + 1].b) // 价格高的放后面
{
needswap=true;
}
else if(s[j].b==s[j+1].b)// 价格相同,名称长的放后面
{
if(s[j].a.length()>s[j+1].a.length())
{
needswap=true;
}
else if(s[j].a.length()==s[j+1].a.length())// 价格相同,名称长的放后面
{
if(s[j].c>s[j+1].c)
{
needswap=true;
}
}
}
if(needswap)
{
student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) // 输出排序结果
cout << s[i].a << " " << s[i].b << " " << s[i].c << endl;
return 0;
}
更多推荐

所有评论(0)