Skip to content

Structures

Structure (struct) can be seen as a combination of a series of elements called members.

It can be considered a custom data type.

Note

The struct described on this page is different from the struct in C. In C++, struct is extended to a class specifier similar to class.

Defining a Structure

1
2
3
4
5
6
7
8
struct Object {
  int weight;
  int value;
} e[array_length];

const Object a;
Object b, B[array_length], tmp;
Object *c;

The above example defines a structure named Object with two member elements value and weight, both of type int.

After the }, a constant a of type Object, variables b and tmp, array B, and pointer c are defined. For any existing type, constants, variables, pointers, arrays, etc., can be defined using this method.

Regarding pointers: mastery is not required.

Defining Pointers

If defining a pointer to a built-in type, it is the same as usual.

If defining a pointer to a structure, use StructName* in the definition.

1
2
3
4
5
6
struct Edge {
  /*
  ...
  */
  Edge* nxt;
};

The above example is for illustration only; do not dwell on its practical meaning.

Accessing/Modifying Member Elements

You can use variable_name.member_name to access members. For example, you can use cout << var.v to output the v member of var.

You can also use pointer_name->member_name or (*pointer_name).member_name to access members. For example, using (*ptr).v = tmp or ptr->v = tmp assigns the member element v of the structure pointed to by the structure pointer ptr to tmp.

Why Use Structures?

First, all roads lead to Rome; you can achieve the same effect without using structures. However, structures can explicitly bundle member elements (usually variables in competitive programming) together, as in the Object structure in this example, which groups value and weight together (the practical meaning of defining this structure is to represent the weight and value of an item). The advantage of this is that it restricts the use of member elements.
Imagine if you didn’t use structures and had two arrays value[], Value[]; it would be easy to confuse them. But using structures reduces the likelihood of variable usage errors.

Moreover, different structures (structure types, such as the Object structure) or different structure variables (instances of structures, such as the e array above) can have member elements with the same name (e.g., tmp.value, b.value). Member elements with the same name are independent (they have their own memory; for example, modifying tmp.value does not affect b.value).
The advantage of this is that you can use as many identical or similar variables as possible to describe an item. For example, Object has a member variable value; we can also define a Car structure that also has a member value. Without structures, we might need to define arrays with different names like valueOfObject[], valueOfCar[] to distinguish them.

If you want a more detailed description of something, you can also define member functions. Please refer to Class for details.

More Operations?

See Class for details.

Notes

To improve memory access efficiency, the compiler may align members at certain byte positions when handling the actual storage of members in a structure, meaning there may be gaps in the structure. Therefore, the space occupied by the structure may be larger than the sum of the spaces occupied by all its members.

References

  1. Class - zh.cppreference.com
  2. Data structures - cplusplus.com
  3. Alignment - Microsoft Docs