Structure in C++ - C++ 中的结构
We have already dealt with arrays. Arrays are used to store similar type of data.
我们已经操作了数组。数组用于存储相似类型的数据。
We use structures to store different types of data. For example, you are a student. Your name is a string and your phone number and roll_no
are integers. So, here name, address and phone number are those different types of data.
我们使用结构体来存储不同类型的数据。例如,您是一名学生。您的名字是一个字符串,您的电话号码和 roll_no
是整数。因此,这里的姓名、地址和电话号码是这些不同类型的数据。
1. Defining a Structure
The syntax for structure is:
syntax [ˈsɪntæks]:n. 语法,句法,有秩序的排列
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
struct structure_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};
In our case, let’s name the structure as student
. The members of the structure in our case are name
, roll_no
and phone_number
.
在我们的例子中,我们将该结构体命名为学生。在我们的案例中,该结构体的成员为 name
, roll_no
and phone_number
。
So, our structure will look like:
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
struct student
{
int roll_no;
std::string name;
int phone_number;
};
2. Declaration of Structure Variable
Just as we declare variables of type int
, char
etc, we can declare variables of a structure as well.
正如我们声明 int
、char
等类型的变量一样,我们也可以声明结构体的变量。
Suppose we want to store the roll no
, name
and phone number
of three students. For this, we will define a structure named student (as declared above) and then declare three variables, say p1
, p2
and p3
(which will represent the three students respectively) of type student
. This declaration will be done in the main
function.
假设我们要存储三个学生的 roll no
, name
and phone number
。为此,我们将定义一个名为 student
(如上所述) 的结构体,然后声明类型为 student
的三个变量,例如 p1
, p2
and p3
(分别代表三个学生)。该声明将在 main
函数中完成。
roll [rəʊl]:n. 卷,打滚,翻滚,名单 v. 滚动,翻转,摇晃,(使) 原地转圈
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
struct student
{
int roll_no;
std::string name;
int phone_number;
};
int main()
{
struct student p1, p2, p3;
return 0;
}
We can also declare structure variables at the time of defining the structure as follows.
我们还可以在定义结构体时声明结构变量,如下所示。
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
struct student
{
int roll_no;
std::string name;
int phone_number;
} p1, p2, p3;
Now, let’s see how to enter the details of each student
i.e. roll_no
, name
and phone number
.
现在,让我们看看如何输入每个 student
的详细信息,即 roll_no
, name
and phone number
。
Suppose we want to assign a roll number
to the first student. For that, we need to access the roll number
of the first student. We do this by writing
假设我们要为第一个学生分配一个卷号。为此,我们需要访问第一个学生的卷号。我们通过写来做到这一点
p1.roll_no = 1;
This means that we use dot (.
) to use variables in a structure. p1.roll_no
can be understood as roll_no
of p1
.
这意味着我们使用点 (.
) 在结构体中使用变量。p1.roll_no
可以理解为 p1
的 roll_no
。
Now, let’s store the details of all the three students.
现在,让我们存储所有三个学生的详细信息。
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
struct student
{
int roll_no;
string name;
int phone_number;
};
struct student p1 = { 1, "Brown", 123443 };
struct student p2, p3;
p2.roll_no = 2;
p2.name = "Sam";
p2.phone_number = 1234567822;
p3.roll_no = 3;
p3.name = "Addy";
p3.phone_number = 1234567844;
cout << "First Student" << endl;
cout << "roll no : " << p1.roll_no << endl;
cout << "name : " << p1.name << endl;
cout << "phone no : " << p1.phone_number << endl;
cout << "Second Student" << endl;
cout << "roll no : " << p2.roll_no << endl;
cout << "name : " << p2.name << endl;
cout << "phone no : " << p2.phone_number << endl;
cout << "Third Student" << endl;
cout << "roll no : " << p3.roll_no << endl;
cout << "name : " << p3.name << endl;
cout << "phone no : " << p3.phone_number << endl;
return 0;
}
Output
First Student
roll no : 1
name : Brown
phone no : 123443
Second Student
roll no : 2
name : Sam
phone no : 1234567822
Third Student
roll no : 3
name : Addy
phone no : 1234567844
struct student p1 = {1,"Brown",123443};
- This line is just to show that we can also initialize a structure in this way.
该行只是为了表明我们也可以通过这种方式初始化结构体。
Structures use continuous memory locations.
结构体使用连续的内存位置。
3. Array of Structures
We can also make an array of structures. In the first example in structures, we stored the data of 3 students. Now suppose we need to store the data of 100 such children. Declaring 100 separate variables of the structure is definitely not a good option. For that, we need to create an array of structures.
我们还可以制作结构体数组。在结构体的第一个示例中,我们存储了 3 个学生的数据。现在假设我们需要存储 100 个这样的孩子的数据。声明结构体的 100 个独立变量绝对不是一个好选择。为此,我们需要创建一个结构体数组。
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
int roll_no;
string name;
int phone_number;
};
int main()
{
struct student stud[5];
int i;
for (i = 0; i < 5; i++)
{
// taking values from user
cout << "Student " << i + 1 << endl;
cout << "Enter roll no" << endl;
cin >> stud[i].roll_no;
cout << "Enter name" << endl;
cin >> stud[i].name;
cout << "Enter phone number" << endl;
cin >> stud[i].phone_number;
}
for (i = 0; i < 5; i++)
{
// printing values
cout << "Student " << i + 1 << endl;
cout << "Roll no : " << stud[i].roll_no << endl;
cout << "Name : " << stud[i].name << endl;
cout << "Phone no : " << stud[i].phone_number << endl;
}
return 0;
}
Output
Student 1
Enter roll no
11
Enter name
cheng
Enter phone number
111111
Student 2
Enter roll no
22
Enter name
yong
Enter phone number
222222
Student 3
Enter roll no
33
Enter name
qiang
Enter phone number
333333
Student 4
Enter roll no
44
Enter name
forever
Enter phone number
444444
Student 5
Enter roll no
55
Enter name
strong
Enter phone number
555555
Student 1
Roll no : 11
Name : cheng
Phone no : 111111
Student 2
Roll no : 22
Name : yong
Phone no : 222222
Student 3
Roll no : 33
Name : qiang
Phone no : 333333
Student 4
Roll no : 44
Name : forever
Phone no : 444444
Student 5
Roll no : 55
Name : strong
Phone no : 555555
Here we created an array named stud
having 5 elements of structure student. Each of the element stores the information of a student. For example, stud[0]
stores the information of the first student, stud[1]
for the second and so on.
在这里,我们创建了一个名为 stud
的数组,该数组具有 5 个学生结构体元素。每个元素都存储学生的信息。例如,stud[0]
存储第一个学生的信息,stud[1]
存储第二个学生的信息,依此类推。
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
int roll_no;
string name;
int phone_number;
};
int main()
{
struct student p1 = { 1, "Brown", 123443 };
struct student p2;
p2 = p1;
cout << "roll no : " << p2.roll_no << endl;
cout << "name : " << p2.name << endl;
cout << "phone number : " << p2.phone_number << endl;
return 0;
}
Output
roll no : 1
name : Brown
phone number : 123443
We just have to write p1 = p2
and that’s it. By writing this, all the elements of p1 will get copied to p2.
通过编写此代码,p1
的所有元素将被复制到 p2
。
4. Pointers to Structures
Like we have pointers to int
, char
and other data-types, we also have pointers pointing to structures. These pointers are called structure pointers.
就像我们有指向 int
、char
和其他数据类型的指针一样,我们也有指向结构体的指针。这些指针称为结构体指针。
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
struct structure_name
{
data-type member-1;
data-type member-1;
data-type member-1;
data-type member-1;
};
int main()
{
struct structure_name *ptr;
}
Let’s see an example using structure pointer.
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
string name;
int roll_no;
};
int main()
{
struct student stud = { "Sam", 1 };
struct student *ptr;
ptr = &stud;
cout << stud.name << stud.roll_no << endl;
cout << ptr->name << ptr->roll_no << endl;
return 0;
}
Output
Sam1
Sam1
struct student *ptr;
- We declared ptr
as a pointer to the structure student.
我们声明了 ptr
作为指向结构体学生的指针。
ptr = &stud;
- We made our pointer ptr
to point to the structure variable stud
. Thus, ptr
now stores the address of the structure variable stud
.
我们使指针 ptr
指向结构体变量 stud
。因此,ptr
现在存储结构体变量 stud
的地址。
This is the same which we do while defining a pointer to any other variable.
这与定义指向任何其他变量的指针时所做的相同。
cout << ptr->name << ptr->roll_no << endl;
- We use ->
operator to access the members of a structure using a pointer to that structure.
我们使用 ->
运算符通过指向结构体的指针来访问该结构体的成员。
5. Structure to Function
We can also pass a structure to a function.
我们还可以将结构体传递给函数。
There are two methods by which we can pass structures to functions.
我们可以通过两种方法将结构体传递给函数。
Passing by Value
Passing by Reference
5.1 Passing by Value
In this, we pass structure variable as an argument to a function. Let’s see an example to make it clearer.
在此,我们将结构体变量作为参数传递给函数。让我们看一个使它更清楚的例子。
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
int roll_no;
string name;
int phone_number;
};
void display(struct student st)
{
cout << "Roll no : " << st.roll_no << endl;
cout << "Name : " << st.name << endl;
cout << "Phone no : " << st.phone_number << endl;
}
int main()
{
struct student s;
s.roll_no = 4;
s.name = "Ron";
s.phone_number = 888888;
display(s);
return 0;
}
Output
Roll no : 4
Name : Ron
Phone no : 888888
In this example, we are printing roll number
, name
and phone number
of a student using a function. We first declared a structure named student with roll_no
, name
and phone number
as its members and s
as its variable. Then we assigned the values of roll number
, name
and phone number
to the structure variables. Just as we pass any other variable to a function, we passed the structure variable s
to a function display
.
在此示例中,我们使用函数打印学生的 roll number
, name
and phone number
。我们首先声明了一个名为 student
的结构体,其中 roll_no
, name
and phone number
为其成员,变量为 s
。然后,我们将 roll number
, name
and phone number
的值分配给结构体变量 s
。正如我们将其他任何变量传递给函数一样,我们也将结构变量 s
传递给函数 display
。
Now, while defining the function, we passed a copy of the variable s
as its argument with struct student
written before it because the variable which we have passed is of type structure named student
. Finally, in the function, we printed the name
, roll number
and phone number
of the structure variable.
现在,在定义函数时,我们传递了变量 s
的副本作为其参数,并在其前面写入了 struct student
,因为传递的变量是一个名为 student
的类型结构体。最后,在函数中,我们打印了结构体变量的 name
, roll number
and phone number
。
5.2 Passing by Reference
In passing by reference, the address of a structure variable is passed to a function. In this, if we change the structure variable which is inside the function, the original structure variable which is used for calling the function changes. This was not the case in calling by value.
通过引用传递时,结构体变量的地址传递给函数。在这种情况下,如果我们更改函数内部的结构体变量,则用于调用该函数的原始结构变量将发生变化。按值调用不是这种情况。
//============================================================================
// Name : std::struct
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
int roll_no;
string name;
int phone_number;
};
void display(struct student *st)
{
cout << "Roll no : " << st->roll_no << endl;
cout << "Name : " << st->name << endl;
cout << "Phone no : " << st->phone_number << endl;
}
int main()
{
struct student s;
s.roll_no = 4;
s.name = "Ron";
s.phone_number = 888888;
display(&s);
return 0;
}
Output
Roll no : 4
Name : Ron
Phone no : 888888
This case is similar to the previous one, the only difference is that this time, we are passing the address of the structure variable to the function. While declaring the function, we passed the pointer of the copy st
of the structure variable s
in its parameter. Since the pointer is of a variable of type structure named student, we wrote struct student
before the name of the pointer in the argument of the function. In the function , we accessed the members of the pointer using ->
sign as discussed before.
这种示例与前一种示例相似,唯一的区别是这一次,我们将结构变量的地址传递给了函数。在声明函数时,we passed the pointer of the copy st
of the structure variable s
in its parameter。由于指针是一个名为 student
的类型结构变量,因此我们在函数参数的指针名称之前写了 struct student
。在函数中,如前所述,我们使用 ->
符号访问了指针的成员。
Try to change the value of the variables inside the function in both the cases and see the changes in the real variables.
尝试在两种情况下都更改函数内部变量的值,并查看实际变量中的更改。