
Part One: Vectors, Deques and Lists - Chris Barry
![]()
Any computer program lives or dies depending on how its data are organized.
Not at runtime, but while it's being coded and developed. Any program, video game or otherwise, has one purpose - to manage data. Those data can take the form of 3D vertices, bitmaps, enemy classes, tile maps, or anything else. A good programmer will find a way to organize all the data the program needs so it's easy to code, and the code is easy to maintain, read, and reuse. How many newbies get frustrated dealing with their own code because of poor organization? Say you're storing 5000 vertices for a 3D model. Do you make them individual variables with their own names? Or store them in an array? Well, that was a stupid example, but you get the idea. It's gotten to the point where certian algorithms have been found for storing data that are downright universal. Linked lists, anyone? Ever heard of a BSP tree? Hash tables? Articles and tutorials on them can be found all over the place. They're so well known and common you'd think they were standard algorithms.
Actually ... they are.
The standardization of C++ was started in 1989. When it was ultimately finished in 1998, there was more to C++ than the basic syntax. C++ now also has a standard library of functions and classes. This library is chock full of programming goodness, and addresses issues all programmers generally have to deal with. For example, the string class. No more must text be treated as arrays of char; now they have their own class, allowing a programmer to sort, copy, assign and compare text easily without resorting to "C-strings" (arrays of characters). You can even turn a string into a C-string with one function call, so your C++ code can still interact with C code (I do that with Allegro functions that take char * parameters all the time). These operations are mostly done with overloaded operators, making them easy to understand and code. This functionality was added to the Standard Library at the request of programmers who wanted text to be easier to work with. The Standard Library is a bunch of header files normally found in a compiler's include folder. If you want to use strings, you must include the appropriate part of the library:
#include <string>All parts of the Standard Library must be included, because they're not part of the normal syntax. They're functions kept in header files. How many people recognize this?
#include <iostream>
int main()
{
cout << "Hello, world!";
}
You can't use cout without including the header file for I/O streams, which tells the compiler what cout is. A newbie C++ programmer who's only familiar with the basic syntax might be surprised to find what's in their compiler's include folder. A smart-pointer class that automatically destroys resources when it goes out of scope? A string class that practically makes char arrays obsolete? File IO? Data structures? Algorithms? And, of course, our subject for today, the Standard Template Library, or STL.
The STL is a collection of classes that organize data in different ways. There are several STL containers and each suits different needs. For example, in some cases a coder may use arrays to organize information, in others he may use linked lists. Each has its pros and cons. We're going to take a look at each container individually, and see what makes each one tick.
Every now and again I see "How do I make a linked list in C++?" in one forum or another. The newbies have a modest shock coming to them. Open your compiler's include folder. Fair bit of stuff in there, huh? Now, look for a file called list.h. Might be in a folder in your include folder, DJGPP has it in djgpp/lang/cxx but it'll be in your compiler's folders somewhere.
Now guess what that is.
G'head, guess!
C'mon!
Give up?
It's a linked list class!
And not just any old linked list. It works with any data type, including your own classes and structures. It's totally portable (it's not called the Standard Library for nuthin'). Any typical linked list algorithm you care to name is here, some implemented using overloaded operators. Even if you need more functionality, it's a class like any other: derive a new one, add your extra functionality and there you go. And the hell of it is, you don't even need to know how a linked list works in order to use it. You want to delete a node? Call erase(). Want a new one? Call insert(). Period. All pointer reassignment and such is handled by the class.
Now, don't get me wrong. I think it's a good idea to understand how linked lists work. Remember how I said linked lists have their pros and cons? You should understand how these containers handle data, and we'll take a look at that as we go. But when you get to the point where arrays just don't cut it anymore and you need to learn a more advanced algorithm, you have two choices.
1) Search on GameDev and Google for linked list tutorials and articles. Ask stoopid newbie questions on the forums while 13-year-old gurus laugh at you. Get some code from someone, work on it so it works with your code, maintain it, handle bugs in the code...
2) Type #include <list>. Take care not to pull a finger muscle.
I believe I've sold you. Shall we begin?
Next: Vectors