About Me

My photo
I'm a colonist who has declared war on machines and intend to conquer them some day. You'll often find me deep in the trenches fighting off bugs and ugly defects in code. When I'm not tappity-tapping at my WMD (also, known as keyboard), you'll find me chatting with friends, reading comics or playing a PC game.

Monday, March 3, 2008

An introduction to the STL : Vectors

(Reader Level : Beginner)
(Knowledge assumptions : Arrays)

Today I'm going to cover a pretty basic standard library provision that I feel every C++ programmer should know about - Standard Template Library Vectors. Vectors (don't mind the wierd sounding name) are similar to C-style arrays but with more safety. Whenever I explain a concept, I prefer to illustrate what I mean with some code. Let's first try to create a one-dimensional C-style array of type int:
#include <iostream>

int main()
int myArray[3];

myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
//myArray[3] = 40; //Can't add any more.

for(int i = 0; i < 4; ++i) //Out of bounds!

return 0;

One of the biggest drawbacks of C-style arrays is that the dimension(s) of an array must be known at the time of declaration. This generally leads to wastage of allocated space. In our example, we can't add more than 3 integers to the array because we've already decided on its maximum size at compile time.
Another shortcoming of arrays is that its very easy to trip up and access beyond the bounds of an array. In our example, we inadvertently tried to print the value of myArray[3] simply because its quite hard to remember a hard-coded dimension. Of course, there's lots of other ways by which an out-of-bounds access can happen but we'll just stick to the basics for the time being.

Alright then, let's rewrite the exact same code as above, only this time we'll use Vectors.
#include <iostream>
#include <vector>

int main()
std::vector<int> myVector;

myVector.push_back(30); //Add some more if you like. :)

for(std::vector<int>::size_type i = 0; i < myVector.size(); ++i) //Safer...
return 0;

At the top we include the pre-compiled header file for using vectors. In main(), we declare a vector of integers called myVector. By default, the initial size of myVector will be zero which means that there is no memory allocated for it. Note that, vectors are also named within the std namespace just as cout is. After we declare the vector, we then add data to this vector using a function named push_back(). Since this is an integer vector, we push integers into the vector. This means that at the end of the three pushes, our vector will be as follows:
myVector[0] = 10;
myVector[1] = 20;
myVector[2] = 30;

However, we couldn't just substitute these statements in place of the three push_back() function calls. Why? Remember that the size of myVector was zero when we declared it. So, trying to access an element of the vector through an index will lead to an out-of-bounds access. Finally, we iterate through all the elements in the vector and display the value of each element. The function size() gives us the number of elements in the vector. So, there's no way we could iterate beyond the bounds of the vector. Now you may ask, what's with that wierd data type with which the variable 'i' was declared? It's nothing but a defined type that is implementation specific for vectors. Since we are iterating over the elements of the vector based on its size, the maximum value of the variable i can be the maximum size for any vector.

There are lots of other cool things one could easily do with vectors. For example, you could resize a vector(doing that with an array would be a major headache) or clear the contents of the vector in one go. Unfortunately, all those cool things are beyond the scope of this measly blog post. The following links should help you get started on STL Vectors:
A Beginner's Tutorial For std::vector, Part 1
Member functions of STL Vector
In my next post, I hope to introduce you to another useful STL container.

1 comment:

ravi krishnan unni said...

this post is extremely useful for a beginner like me. Thanks a lot.