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.
There are times where we could use either a vector or a struct to store a collection of data. 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. But usually, one of the two is a better choice. Here are some guidelines to help you decide which to use:
  • If all the items are a different type, you need a struct.
  • If the number of items can vary, a vector is the better tool.
  • If it is natural to refer to items by name, a struct might be preferred. For a point, it might be more natural to use p1.x instead of p1.at(0).
  • If you want to loop through items or pick them via a value stored in a variable, a vector is more appropriate.
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:
Listing 15.9.2.
// 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);

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 should use the size() member of the vector that is inside type1
You have attempted of activities on this page.