LAB 08A - 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 08.

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.