Hello, I’m Liang Tang.

This is EasyC++ series 13, let’s talk about C++ structure.

If you want a better reading experience, you can visit the Github repository.

The structure of the body

Defining structure

Arrays can store multiple variables of the same type, but what if we want to store multiple variables of different types? Let’s say we want to store a student’s name, age, gender, test score, all of these variables might be string, int, float, but we can’t use arrays.

To meet such storage requirements, we can use structs in C++. Within the same structure, we can define many different types of variables, which can meet our various needs. If we want to store more than one of these things, we can also define it as an array.

Structures and classes are already very close, so understanding structures is very helpful in understanding object orientation. Although object orientation is not usually used in algorithmic contests, it is a must-learn for developers. According to C++ primer, constructs are the cornerstone of C++ OOP.

We use the keyword struct to define a structure:

struct student {
	string name;
    bool	gender;
    double scores;
};
Copy the code

Indicates that a structure is defined, whose type name is student, and the contents enclosed in curly braces are the member variables of the structure. Note that student is the type name, which means we can use it to define variables of type student:

student xiaoming;
student john;
Copy the code

We can use. To access elements inside a structure:

cout << john.name << endl;
Copy the code

Struct definitions can be written either outside or inside main. Such as:

/ / write 1
struct student {
	string name;
    bool	gender;
    double scores;
};

int main(a) {
    // do something
    return 0;
}

/ / write 2
int main(a) {
    struct student {
        string name;
        bool	gender;
        double scores;
    };
    // do something
    return 0;
}
Copy the code

Logically the two methods are exactly the same, except that the student type can be used by any function, but only inside main if written inside main.

Struct variables can also be defined inside a function. Variables defined outside the function are shared by all functions, i.e. global variables. C++ primer advocates the use of external constructs wherever possible.

Initialization mode

We can delimit arrays with commas inside curly braces, as in:

student xiaoming = {"xiaoming".1.3.5};
Copy the code

The compiler assigns “xiaoming” to name, 1 to gender, and 3.5 to score, respectively. We can also omit the middle equals sign using C++11’s list initializer:

student xiaoming {"xiaoming".1.3.5};
Copy the code

Struct array

Once a structure is defined, we can also define an array of structures just like the underlying variable types.

struct student {
	string name;
    bool	gender;
    double scores;
};

student sts[10];
Copy the code

For struct arrays, we can also initialize them using list initializers. Since the struct itself is initialized with curly braces, arrays are initialized with nested curly braces, like this:

student sts[2] = {{"xiaoming".1.3.6},
    {"john".1.5.2}};Copy the code