Saturday, July 24, 2010

C++: Introduction to vectors

Personally, I felt that last post was a bit of a departure from what we've
thought of as declaring a variable - we were talking about arrays. This
time, it is yet another departure from what we were used to working with
data, and that's vectors.

A vector is a group of related data, like an array. But unlike arrays,
vectors allows us to resize it, add more items or removes ones from the end
of it and so forth. It has other goodies, such as finding out how many items
are on a vector, looking up the first and last element of it and so forth
via functions. Now why am I mentioning this? Because vectors are an example
of a data type that uses functions a lot, which will be the next grand march
after vector operations are covered.

In this post, I'll not expose you to inner workings of how vectors work and
what kind of useful functions are availible to work with it. That discussion
is reserved for somewhere else, and that is when we are designing our own
vector class as a practice exercise later. For now, I'll go over how to
declare a vector, add a new item and how to display it using several
functions (which I think would provide a gentler intro to functions).

First, before we even declare a vector, we need to include a new library,
logically called vector. So whenever we work with it, at the top of our
program file (.cpp), we need to write:
#include <vector>
This allows us to work with vectors and utilize functions associated with
it. Then to declare a vector, we would write:
vector<type> vector_name;
where "type" is the data type that a vector would hold. For example, if we
want to work with a series of strings, we would write:
vector<string> string_vec; // A vector of strings.
This creates a sufficiently large vector to hold about several string data.
There are a number of variations of the above declaration, including
provision for telling the program how many data we need to use, as well as
setting each data to a default value. For example, if we want to work with
ten integers stored in a vector, we would say:
vector<int> my_integers (10); // A vector with ten ntegers.
And if we want to go further and assign zero to each of the integer entries,
we can write:
vector<int> my_integers (10, 0);
The first variant just sets up a vector array with ten spaces to be filled
in (actually, they are set to a predetermined value); the number of items
must go inside the parenthesis. The second version goes further and declares
that we want to fill ten spaces with 0's, with each argument separated by a
comma. I'll describe how this magic is performed much later.

Now we have our vector, how do we know the size of it? We can use a helper
assistant to tell us how many items are in a vector (this is called a member
function, which I'll go over later). For example, if we want to find out how
many items are there on my_integers vector, we can write:
my_integers.size();
The syntax above is the default form where the variable name is followed by
a dot (period), then the function name. So, using the example above, if we
use this code in our program, it should tell us that there are ten items.

Now, how to we add additional items to our vector? You may recall that
arrays does not allow its size to be changed by adding new items; in
vectors, we can by using another member function called push_back. This one
alloows us to add additional item by putting the actual item within
parenthesis, like this:
my_integers.push_back(11);
In other words, we have just added a new item, 11 as our eleventh item in
our my_integers vector.

Now, how about if we want to display items in this vector? Just like arrays,
items in a vector can be accessed using the subscript operator ([]). Also,
note that we can use a "for" loop to display the vector items. A rough
English version is this:
First, we want to set up a for loop so that it would print from the
beginning of a vector until all items were printed. Thanksfully, we do know
how many items are there in our vector via size() funciton, so we can use
that as our termination indicator. Then we can increment our output by one
until everything is displayed.
In C++, it would go something like this:
// A complete program to print out a vector.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// First, get some numbers to be stored on an integer vector.
// Let's create a vector with five elements, all initialized to the value
10.
vector<int> ten (5, 10);
// Let's display the vector.
for (int i = 0; i < ten.size(); i++)
{
cout << ten[i] << " ";
}
cout << endl;
return (0);
}
Quite a review here. We've noted that we can use the size function to find
out how many elements does a vector have, then we can use subscript operator
to access each element. If we run this program, it would print the value 10
five times.

The reason why I'm stopping the discussion of vectors here is that most of
our notes here refer to something we haven't covered extensively. This is
functions, which I'll talk about next.
// JL