LAB 09B - MEET THE VECTOR

Concepts

For this assignment, the focus is on one main concept: how to create and use vector objects. Your solution to this lab assignment will be submitted with Homework 09.

Drawbacks to Using Arrays

While arrays are powerful/simple data structures, they have certain constraints that often "annoy" a student of programming. One specific drawback to arrays is that, unless you know about dynamic memory allocation, you have to specify the size of the array at compile time.

For example, let's say you wanted to create a program that reads an unknown number of values from a data file. Would you store them in an array that can hold ten values? Or one thousand? How many data points are in the file? You don't know. More importantly, what should the size of your data structure be? You don't know.

"Ahh, I'll just declare an array that can hold 100,000 values, surely that's plenty," you might think. What happens if the data file contains only 10 data points? You would have allocated a bunch of memory you don't need. What happens if the data file contains more than 100,000 data points? You would be in deep doo-doo.

While there are ways to solve this problem with arrays and dynamic memory allocation, it is far more sane to learn how to use a vector object.

vector Objects

You were introduced to vectors and some ways in which you may use them in class, so we'll simply summarize the details here.

First, recognize that you must include the vector library:

#include <vector>

Next, you need to specify what type of vector you want. For example, to create a vector that holds, say, character values, you need to type this "strange" syntax:

vector<char> myLetters;

This is like telling the computer, "Hey computer, myLetters is a vector that can hold char values." Say that five times aloud to disturb the people around you. Better yet, shout it, and watch everyone really freak out.

Notice that the datatype of myLetters is "a vector that can hold chars." Also notice how you do not need to specify the size of the vector.

Since vectors are objects, they have member functions that you may call. For example, you can push an element onto the end of the vector, pop an element off the end of the vector, ask the vector what it's first/last element is, and request the vector to tell you how many elements it contains.

There are many more features and clever uses of vectors than what we can describe in class. Your goal should be to recognize that vector objects exist, note that they allow greater flexibility than arrays, and learn how to use a vector in a program. The truth is, while arrays are great in multidimensional cases, there are very few people at C++ programming parties today using an array to store a series of values -- most prefer to use vectors because of their flexibility.

Instructions

Write a program that allows the user to enter an unknown number of characters, stores those characters in a data structure (a vector), and then prints the values to the screen. An example interaction looks like this:

Witness my exciting first use of vectors!
Enter as many letters as you like, or ! to quit. Mmmkay?
Enter a letter: b
Enter a letter: u
Enter a letter: r
Enter a letter: r
Enter a letter: p
Enter a letter: !
Great. You entered: burrp
The first character you entered was b.
The last character you entered was p.

Note how the last character typed was an exclamation point and was not printed to the screen. Furthermore, it should not be stored in the vector.

While there are many ways of solving this assignment, you must make use of the following vector member functions: push_back, size, front, and back. See details for these member functions here. FYI: you may define your own functions, but we recommend just entering this simple program entirely in main.