Structs
Structs
Introduction
In many programs, one object is described by several related values. A point has an -coordinate and a -coordinate. An edge can have an endpoint and a weight. A contest participant can have a name, a number of solved problems, and a penalty.
We could store every value in a separate variable or array, but then the connection between them exists only in our head. C++ allows us to define our own type with struct. One object of that type keeps all related values together and gives each value a meaningful name.
Problem and motivation
Suppose we have participants. For every participant, we know:
- their name;
- the number of solved problems;
- their penalty.
We need to print the ranking. A participant comes earlier if they solved more problems. If two participants solved the same number, the one with the smaller penalty comes earlier. If both values are equal, names are compared alphabetically.
For example, the record Madina 4 90 means that Madina solved problems and has penalty . These three values describe one participant, so we want them to stay together throughout the program.