Have you read Effective C++ also by Scott Meyers? No? Go buy it right now, read it, reread it, and then come back here. I guarantee that it will make a huge difference in the way you work.
This review was first published in the January 2002 issue of Game Developer Magazine.
OK, welcome back. Here is the good news: Effective STL is to STL what Effective C++ is to C++. It’s equally great and it’s written in the same light, conversational Scott Meyers style that makes reading it a pleasure. It assumes that you have basic knowledge of STL (and C++) and builds on that to show you how to use it effectively and avoid common pitfalls.
Those of you unfamiliar with STL must be wondering what it is and how is it useful for game development. STL stands for Standard Template Library: A library of generic data structures and algorithms that you can use across many compilers/platforms, including most of the current consoles. The library is implemented with templates so the resulting code will be quite efficient, possibly even more than your hand-coded structures and algorithms. Additionally, it has been implemented, tested, debugged and optimized by thousands of people, so it’s code you can usually rely on. STL also empowers you by putting very powerful constructs at your fingertips. Maybe before you would have thrown a bunch of objects in an array and searched through them in linear time (does that sound familiar?), but now you can just as easily put them in a hash table and have constant-time access to them.
Effective STL starts out with a fairly thorough discussion about containers (vector, list, map, etc) and iterators. It immediately goes beyond the typical description of the containers and their O(n) performance characteristics. Instead it deals with many real-world questions: Is the memory for the elements allocated contiguously? Are the iterators invalidated when the elements change? What the most efficient way of removing elements for a specific container? Those are not issues you’ll see addressed in most STL books.
It then moves on to algorithms and functors. Just like with the containers, instead of listing all the available algorithms, it points out common mistakes and how to deal with them. For example, it will discuss the different ways of sorting elements, or how std::remove really works (and why it doesn’t really remove anything).
The final chapters present more general, but very useful, information on the STL that will save you a few headaches along the way: When to use STL algorithms and when to use your own, style guidelines, or even how to deal with Microsoft’s Visual C++ broken STL implementation and template support.
But STL is not perfect. Reading the book will give you some ideas of where STL is lacking, but it won’t spell them out for you. Sometimes you’ll need to read between the lines and think about how things will apply to game development. For example, memory allocation can be an issue, especially if you’re developing for a console, so you’ll probably want to end up writing your own allocators. The book has a brief couple of items discussing what you can and can’t do with allocators, but not enough to write your own. You’ll need to look elsewhere for that.
Another issue often brought up when dealing with STL is the difficulty debugging STL code, from cryptic multi-line error messages to the difficulty viewing the elements of a container in the debugger. The book will help you a bit by showing you how to “parse” the intimidating error messages and directs you to some STL resources on the Internet.
Effective STL works very well both as a book to read cover to cover and as a reference later on. It only uses source code where it has to; it won’t bore you with pages and pages of pointless code. As a matter of fact, it won’t bore you at all since it packs a lot of information in a mere 250 pages. Overall, you simply must read this book before you decide to use (or not to use) STL in your next game or tools. If you’re already using STL, then it should already be on your bookshelf.