Excerpts of C++ structure knowledge points

structure

The general form of the structure definition is:

struct <结构名>{
    <成员列表>
};
  • struct is a keyword, which means structure definition;

  • <structure name> is an identifier, the name of the structure;

  • The <member list> consists of declarations of several members, and each member is an integral part of the structure.

For example, a fraction contains two members: numerator and denominator, which can be defined as a structure. The name of the score structure can be named rationalNumber. Both members are integer variables. They can be named fenzi and fenmu respectively. Then the score structure can be defined as:

struct rationalNumber{
    int fenzi;     // 分子
    int fenmu;     // 分母
};

The definition of the structure is to define a new data type (the new type name is the structure name) so that there will be no memory allocation. Then you can use the structure type as a predefined type (such as int).

E.g:

rationalNumber x,y;

This statement declares two variables x and y of type rationalNumber. Each variable contains two members: integer fenzi and fenmu. The variable declaration will have memory allocation. You can use the two integer variable members of x and y to store data, so that the numerator and denominator of the same score can be stored in the corresponding variable of the rationalNumber type, and the variable can represent one Scored.

The definition of the structure can be nested. For example, the person structure contains birthday members, and the birthday contains three members: year, month, and day. The code is as follows:

// 定义结构date
struct date
{
    
    
    int year, month, day;
};
// 定义结构person
struct person
{
    
    
    int IDNumber;     // 身份证号
    char name[20];     // 姓名
    char sex;      // 性别
    date birthday;     // 生日,嵌套定义
};

Initialization and use of structure

The initialization of structure variables is the same as the initialization of array variables, in the form of a list of initial values ​​enclosed in curly braces.
E.g:

rationalNumber x={
    
    12,46};

In this statement, its initial value is assigned to each member in sequence, that is, 12 and 46 are assigned to the fenzi and fenmu members of x, respectively.
There are two ways to use structure variables: one is to use the structure variable as a whole; the other is to disassemble the use and operate one of its members separately:
when the structure variable is used as a whole, there are two operations Symbol: Assignment operator = and address operator & can directly act on structure variables;
for example:

rationalNumber x = {
    
    12,46}, y ;
y = x ;     // 结构变量中的每个成员分别赋值
rationalNumber *p = &x;      // 声明结构指针,并初始化为 x 的地址

When the structure variable is used separately, the members of the structure variable need to be accessed, and the members of the structure variable can be used like ordinary variables. For example, the two members of the score can be regarded as ordinary int type variables.
Among them: there are two ways to disassemble the structure, one is to use structure name + dot operator (.); the other is to use pointer to structure variable + arrow operator (->).

E.g:

rationalNumber x= {
    
    12,46}, y ;
y = x ;     // 结构变量中的每个成员分别赋值
rationalNumber *p = &x;     // 声明结构指针 p,并让 p 指向 x
x.fenzi = 18;     // 使用圆点操作符访问并修改 x 的成员 fenzi
p->fenmu = 108;     // 使用箭头操作符并修改 x 的成员 fenmu

Functions can not only pass in structure variables through parameters, but also return structure variables through return values.
For example: the following add function adds 1 to the numerator and denominator of the fraction x and returns:

#include <iostream>
using namespace std;
struct rationalNumber{
    
    
    int fenzi;     // 分子
    int fenmu;     // 分母
};
rationalNumber add(rationalNumber x)
{
    
    
    x.fenzi++;     // 分子加 1
    x.fenmu++;     // 分母加 1
    return x;     // 返回结果
}
int main()
{
    
    
    rationalNumber a ={
    
    23,56}, b ;
    b=add(a);     // 调用add 函数
    cout << b.fenzi << "/" << b.fenmu << endl;     // 输出返回值
    return 0;
}
  • The output result is: 24/57.
    Warm reminder: As a new data type, structure variables can be used in the same way as predefined types.

Structure array

The definition of the structure is to define a new data type, so you can use the structure name (type) to declare the structure array. Each array element of the structure array is a structure variable.
When using a structured array, first access the array elements by adding a subscript to the name of the array just like using an array. The array element is the structure variable, and then the members of the structure variable (using the dot operator or the arrow operator), the structure variable Members can be used as ordinary variables.
The following program defines the structure student and declares the student type array cs (cs contains 5 elements, namely cs[0], cs[1], cs[2], cs[3], cs[4]), At the same time, the array cs is initialized:

struct student {
    
    
    int num;
    char name[20];
    float score;
}cs[5]={
    
    {
    
    110, ″Zhang Ping″, 45},
        {
    
    102, ″Li Xiaoming″, 92},
        {
    
    153, ″Wang Ming″, 52.5},
        {
    
    134, ″Cheng Ling″, 87},
        {
    
    105, ″Wang Xiaofang″, 95},
};

Since cs is an array, the initialization syntax is 5 data separated by commas enclosed in curly braces. Since each element of the cs array is a structure variable, the initialization syntax for structure variables is the value of each member enclosed in curly braces. .
The first brace part {110, "Zhang Ping", 45} is initialized to cs[0], where 110 is given to cs[0].num, and "Zhang Ping" is given to cs[0].name (actually cs[ 0].name is also an array, which is also initialized one by one.'Z' is given to cs[0].name[0],...), and 45 is given to cs[0].score.

Access structure array members

To use a structured array, it also needs to be disassembled. The array uses loop traversal, and uses [] to add subscripts to access its array elements, and structural variables can be disassembled using the dot operator.
For example, the following program outputs all the information of the array cs:

for(int i = 0; i < 4; i++)
{
    
    
    cout<<cs[i].num<<", "<<cs[i].name<<": "<<cs[i].score<<endl;
}

Guess you like

Origin blog.csdn.net/interestingddd/article/details/114409097
Recommended