Structs and vectors are similar in that they allow us to store multiple values in a package with a single name. But there are some important differences:
Every member of a struct can be of any type, while all members of a vector must be of the same type.
You can use a loop to traverse a vector or a variable to easily store an index. There is no equivalent way to loop through the members of a struct or specify one with a variable.
Thus, some βcollectionsβ of data are clearly best represented as structs. While others are clearly best represented as vectors. If the pieces of data you want to combine are of different types, you need a struct. To store a Person with a name (string) and age (int) you would need a struct. If the items are all the same type but the number of them can vary, you need a vector. If you are storing a list of the lines of text in a file, you would want a vector of strings.
But there are times where we could use either a vector or a struct to store a given set of data. For example, we could store a mathematical point as a struct with two members, x and y, or we could store it as a vector with two elements (where x is at index 0 and y is at index 1). In these cases, we should consider how we will use the data to help decide which is best:
If it is natural to refer to items by name, a struct will let us write code that may be more readable. Say we are storing a Color. It is clearer to use c1.red instead of c1.at(0) to access the amount of red in the color. Thus we should probably prefer a struct.
If you want to loop through items or pick them via a value stored in a variable, a vector is going to be necessary. We know a rectangle only has 4 corners, so we could store them in a struct that has corner1, corner2, corner3, and corner4 members. But it would likely be easier to use a vector of points instead (corners) so we can write algorithms that loop through the points.
Say we want to store students who have names and a list of exam scores. We would like to be able to loop through their scores to do things like calculate an average. We also need to be able to add scores. We could represent those scores as a vector of doubles within a Student struct:
Now a student is defined as something that has a name and an exams. The exams is a list (vector) of values. The memory diagram for a particular student would look something like this:
// Print the first exam score
cout << student1.exams.at(0) << endl;
// Print the number of exams
cout << student1.exams.size() << endl;
// Add a new score
student1.exams.push_back(100.0);
They can store multiple types of data in one package
A vector must be of a single type. You canβt mix types in a vector.
It is easy to loop through the elements.
Correct!
You can add or remove items at runtime.
The members of a struct are fixed at compile time. You canβt add or remove them.
Elements can be given meaningful names.
Elements have indexes, not names. In some situations, 0, 1, 2, ... might be considered meaningful, but you canβt assign other names to identify the elements.
Your answer should have three parts separated by dots: You need to name the struct, the vector inside of it, then access the correct element of the vector.