Struct (struct) can be regarded as a combination of a series of elements called members.
It can be regarded as a user-defined data type.
Note
The struct described on this page differs from struct in C; in C++, struct is extended into a class specifier similar to class.
Defining a struct
struct Object {
int weight;
int value;
} e[array_length];
const Object a;
Object b, B[array_length], tmp;
Object *c;
The example above defines a struct named Object, with two member elements, value, weight, both of type int.
After }, it defines a constant of data type Object, a, a variable b, a variable tmp, an array B, and a pointer c. For any existing type, this method can be used to define constants, variables, pointers, arrays, and so on.
About pointers: do not force yourself to master them.
Defining pointers
If you are defining a pointer to a built-in type, it is defined in the same way as an ordinary pointer.
If you are defining a pointer to a struct, use StructName* in the definition.
struct Edge {
/*
...
*/
Edge* nxt;
};
The above is only an example; there is no need to dwell on its practical meaning.
Accessing/modifying member elements
You can use 变量名.成员元素名 to access a member. For example, cout << var.v can be used to output the var's v member.
You can also use 指针名->成员元素名 or (*指针名).成员元素名 to access a member. For example, (*ptr).v = tmp or ptr->v = tmp can assign tmp to member element v of the struct pointed to by struct pointer ptr.
Why do we need structs?
First of all, all roads lead to Rome: the same effect can be achieved without using structs. However, structs can explicitly bind member elements (usually variables in algorithm competitions) together. For example, the Object struct in this example puts value,weight together (the actual purpose of defining this struct is to represent an item's weight and value). The benefit is that it restricts the use of member elements.
Imagine that, without using a struct, there are two arrays, value[], Value[]; it is easy to confuse them. With a struct, however, the chance of using the wrong variable can be reduced.
Moreover, different structs (struct types, such as the Object struct) or different struct variables (struct instances, such as the e array above) can have member elements with the same name (such as tmp.value,b.value). Member elements with the same name are independent of one another (they have their own memory; for example, modifying tmp.value does not affect the value of b.value).
The benefit is that variables that are as identical or similar as possible can be used to describe an item. For example, Object has a member variable named value; we can also define a Car struct with a value member. Without structs, we might need to define arrays with different names such as valueOfObject[],valueOfCar[] to distinguish them.
If you want to describe something in more detail, you can also define member functions. See classes for details.
More operations?
See classes.
Notes
To access memory more efficiently, when handling the actual storage of members in a struct, the compiler may align members at certain byte positions, which means that there may be unused space in the struct. Therefore, the space occupied by the struct may be larger than the sum of the spaces occupied by all its members.