Skip to main content

Section 15.9 Structs and Vectors

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.
  • The number of items in a struct is fixed by the defined members. A vector can grow or shrink.
  • 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.
So what if you need the strengths of booth?
You can combine the two by using a vector inside of a struct, by making a vector of structs, or even by doing both at once.

Subsection 15.9.1 Structs containing Vectors

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:
struct Student {
  string name;
  vector<double> exams;
};
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:
A student struct with a name and exams fields
Figure 15.9.1.
To create that student, we could use a list of two items, in which the second item is a list to use for the exams:
Student student1 = {
  "Beth Jones",
  { 84.2, 94.3, 96.7 }
};
To access a particular exam score, we would use the dot operator to access the vector and then the dot operator again to use a method of the vector:
// 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);
Listing 15.9.2.

Checkpoint 15.9.1.

Which statements are advantages of structs?
  • They can store multiple types of data in one package
  • Correct!
  • It is easy to loop through the members.
  • You can’t index into a struct with a variable. That makes it impossible to loop through them.
  • 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.
  • Members can be given meaningful names.
  • Correct!

Checkpoint 15.9.2.

Which statements are advantages of vectors?
  • 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.

Checkpoint 15.9.3.

Hint.
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.
You have attempted of activities on this page.