结构体是 C++ 中一种将不同类型 变量 和成员函数组合在一个名称下的集合。
它类似于 类,因为它们都包含不同数据类型的集合。
假设您想存储关于一个人的信息:他们的 first_name(名字)、last_name(姓氏)、age(年龄)和 salary(薪水)。
您可以轻松地创建不同的变量——first_name、last_name、age、salary——来单独存储这些信息。
然而,未来您可能需要存储关于多個人的信息。
现在,您需要为每个人、每项信息创建不同的变量:first_name1、last_name1、age1、salary1、first_name2、last_name2、age2、salary2,依此类推……
您可以想象一下代码会有多大、多混乱。此外,由于变量(信息)之间没有关联,管理起来将是一项艰巨的任务。
一个更好的方法是将所有相关信息打包到一个名称下,例如 Person,然后为每个人使用这个结构。
现在,代码看起来更清晰、更易读,效率也更高。
这个将所有相关信息打包到一个名称 Person 下的集合就是一个结构体。
如何在 C++ 编程中声明结构体?
struct
关键字 定义一个结构体类型,后面跟着一个 标识符(结构体的名称)。
然后,在花括号内,您可以声明该结构体的一个或多个成员(在花括号内声明变量)。例如:
struct Person
{
string first_name;
string last_name;
int age;
float salary;
};
这里,定义了 Person 结构体,它有四个成员:first_name、last_name、age 和 salary。
当定义一个结构体时,不会分配任何内存。
结构体定义只是创建变量的蓝图。您可以将其视为一种数据类型。
如下定义一个整数时:
int foo;
int
指定变量 foo 只能包含整数。同样,结构体定义也只指定当结构体变量定义时它包含哪些属性。
注意:请记住在声明末尾加上分号 (;)。
如何定义结构体变量?
一旦您按照上面的方式声明了 Person 结构体,您就可以这样定义一个结构体变量:
Person bill;
这里,定义了一个 bill 结构体变量,它的类型是结构体 Person。
只有在声明结构体变量时,编译器才会分配所需的内存。
如何访问结构体的成员?
结构体变量的成员使用点 (.) 运算符来访问。
假设您想访问结构体变量 bill 的年龄并为其赋值为 50。您可以使用以下代码完成此任务:
bill.age = 50;
示例:C++ 结构体
// Program to assign data to members of a structure variable
#include <iostream>
using namespace std;
struct Person
{
string first_name;
string last_name;
int age;
float salary;
};
int main()
{
Person p1;
cout << "Enter first name: ";
cin >> p1.first_name;
cout << "Enter last name: ";
cin >> p1.last_name;
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "First Name: " << p1.first_name << endl;
cout << "Last Name: " << p1.last_name << endl;
cout << "Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0;
}
输出
Enter first name: Jane Enter last name: Smith Enter age: 27 Enter salary: 10000 Displaying Information. First Name: Jane Last Name: Smith Age: 27 Salary: 10000
这里声明了 Person 结构体,它有四个成员:first_name、last_name、age 和 salary。
在 main()
函数内部,定义了一个结构体变量 p1。然后,要求用户输入信息,并显示用户输入的数据。
C++ 结构体中的成员函数
在 C++ 中,结构体也可以包含成员函数。
这些成员函数类似于常规函数,但在结构体的作用域内定义。它们可以直接访问和操作结构体的数据成员。
我们可以通过在结构体定义内部定义函数来声明成员函数。
struct Person {
string first_name;
string last_name;
int age;
float salary;
// member function to display information about the person
void displayInfo() {
cout << "First Name: " << first_name << endl;
cout << "Last Name: " << last_name << endl;
cout << "Age: " << age << endl;
cout << "Salary: " << salary << endl;
}
};
在这个例子中,Person
结构体包含一个名为 displayInfo()
的成员函数,该函数显示关于该人的信息。
让我们看一个例子。
#include <iostream>
using namespace std;
struct Person {
string first_name;
string last_name;
int age;
float salary;
// member function to display information about the person
void display_info() {
cout << "First Name: " << first_name << endl;
cout << "Last Name: " << last_name << endl;
cout << "Age: " << age << endl;
cout << "Salary: " << salary << endl;
}
};
int main() {
Person p1;
cout << "Enter first name: ";
cin >> p1.first_name;
cout << "Enter last name: ";
cin >> p1.last_name;
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
// display information using member function
cout << "\nDisplaying Information." << endl;
p1.display_info();
return 0;
}
输出
Enter first name: Jane Enter last name: Smith Enter age: 27 Enter salary: 10000 Displaying Information. First Name: Jane Last Name: Smith Age: 27 Salary: 10000
另请阅读