Thursday, July 29, 2010

C++: A detailed tour of vector operations

Last time, we looked at how to manipulate strings and learn what a member
function is and how it is used. In this post, let's revisit the concept of a
member function using vectors.

Just like strings, in order to use member functions for vectors, we need to
include vector library:
#include <vector>
Just like strings, we can manipulate vectors, such as find out how many
items are in a given vector, access elements, add or remove something and so
forth. Let's examine it in more detail.

To declare a vector, you would use the syntax like:
vector<type> var;
Where "type" is the data that the vector would hold, and "var" is the name
of this vector. For instance, we can declare a string vector like:
vector<string> s;
Note the arrows (less-than and greater-than signs) surrounding our data
type. As a matter of fact, there are two other popular variants of our above
code - one is used to tell the computer how many items will be stored on
that vector, and another one goes further and assigns each element to a
unique value. For instance, if we want to store five integers in a vector
called "item", we could use:
vector<int> item (5);
Furthermore, if we want to assign the value "10" to each of the elements, we
could say:
vector<int> item (5, 10);
In our second declaration, what happens if we output our vector using for
loop?
for (int i = 0; I < 5; i++)
{
cout << Item[i] << " ";
}
On the screen, the result would be five "10's" being displayed on the
monitor, just like our output routine from our example earlier on another
post.

But we don't really want to use a fixed number for this kind of output code.
What if the number of items change? Thankfully, there is a dedicated
assistant that helps us here: the "size" member function, which gives us how
many elements does a vector have (you may have seen it before): v.size();
So, if we use this function, we'll get the output code similar to something
you may have seen before:
for (int I = 0; I < item.size(); i++)
{
cout << item[i] << " ";
}
And, as I've said before, we use subscript operator ([]) to access elements.

As for adding and removing things, in addition to the push_back function
we've seen, there is a dedicated function to take out the last element of a
vector: the pop_back member function. The job of this function is to delete
the last element in a vector and decrease the size of our vector by one. We
call this function similar to how we call size:
v.pop_back();
Don't worry about arguments here - it does not need it. So, for instance, if
I invoke the pop_back on my item vector, it would delete the fifth "10" and
decrease the size by 1 (so the total would be four numbers):
Item.pop_back();
Now, if we invoke the for loop to print our vector, it should print four
"10's" instead of five. I guess that's enough on vectors for now - enough
basic functions and tips to get you started on a small program to work with
vectors.

By now you may be wondering why I am giving you tips on so-called member
functions. This was done so that I can gradually introduce you to the
concept of objects and classes, or custom (or programmer defined) data
types. I'll provide more details on my decision and flow of our discussion
on the next post. Then you can have a hands-on experiments with working with
member functions.

// JL

No comments:

Post a Comment