Effective STL
50 specific ways to improve your use of the Standard Template Library
If you’re an experienced C++ programmer, you’ve most probably heard the name Scott Meyers, moreover, you’ve thanked him for his brilliant books. The “Effective STL” is one of them.
If you’re new to the C++ and you were trying to understand whether this book (or STL in general) is worth checking out, stay tuned!
The Standard Template Library (STL) is the language built-in library for C++. It consists of perfectly designed and implemented utilities. I’ll go ahead and say that it’s more than a library: it’s a whole world with its philosophy and rules, a world full of opportunities. As a rule of thumb, the stronger the tool, the harder it is to master. That’s where this book comes to help.
The book consists of 7 chapters. STL provides four types of components: containers, algorithms, iterators, and functors.
The first three chapters cover the containers in STL. Note that each chapter consists of items. Each item is a single piece of advice. The title of an item is an imperative: “Always do this” or “Never do that”, or even “Consider doing this”. This design splits the whole book into smaller, concrete and easy-to-digest chunks. A wide variety of topics are discussed in these three chapters, from the choice of containers to weird C++ syntax quirks. After these chapters you’ll have a better understanding of the containers, their underlying abstract data structures and possible implementations, will have learned some cool tricks and good practices, also some things you should never do.
The book is from 2001 and a lot of things have changed since then. The Item 25 talks about the nonstandard hashing containers, how unfortunate it is that STL lacks them and how to fill the void. Even though, since C++11 we have “unordered” hashing containers, this item is still worth reading.
The next, fourth chapter covers the iterators. It gives a good understanding of the overall concept and idea behind the iterators, teaches some cool tricks and practical usages.
The next, fifth chapter covers the algorithms. If you’re one of those people who are scared of this word, relax: there is no math here. The whole idea of STL was to take that responsibility and implement those algorithms in the best possible way. All you have to do is to use them correctly. This chapter covers the majority of STL algorithms, shows the common mistakes and teaches how to avoid them, teaches the importance of the naming and the philosophy behind (for example the difference between remove and erase).
By the way, if you really are one of those people, don’t be. Algorithms are awesome. Go check out this book.
The next chapter covers the functors, functor classes, etc. These components complete the algorithms and thus are very important too. As we have std::function
and lambdas since C++11, some of the items in this chapter might seem expired, and yes, maybe there is no practical use of it anymore, but it’s quite important to understand the “under the hood” of these new features, especially the lamdas. And of course, there are still projects that are stuck with C++98, so it’s definitely worth learning.
The last chapter is a summary of the STL, gives some practical tips and, more importantly, teaches the philosophy of STL, sets the right mindset. It’s very important to have the right guidance into the world of STL, not only to be safe from mistakes, but also to get the maximum out of it.
This book is a masterpiece for a number of reasons. First and foremost, it’s structure is brilliant. It consists of items, each of which is a 5 to 10 minutes-long article explaining a single idea, with an imperative title. Every so often I pick up this book and go through the table of contents:
- “Item 1: Always do this.” — “Well of course.”
- “Item 2: Never do that.” — “Fair enough.”
- “Item 3: Consider this.” — “I do consider it.”
- “Item 4: Never do that.” — “Ehm, I remember I shouldn’t, but … why exactly?”
And then I go ahead and refresh my knowledge. So it’s also like a handbook. Not only you can, but you should revisit this book again and again, because of another great aspect of it. It’s written for everyone. Doesn’t matter if you’re a novice or experienced, you need this book. You’ll get different things from it.
This book (and others from Scott Meyers) became a source of great interview questions, and everyone knows it, so it’s become a must before the interviews. First time I read this book before my first interviews. At that time I didn’t have much experience with STL, so I would simply read the item, try to keep them in mind. It was not until I revisited the book two years later that I understood I had not read it.
I highly recommend to read or reread this book after you have some experience with C++ and STL. The writing is very entertaining, it makes you think. Almost every item starts with a problem, a very real-life one. Then it challenges you to solve it, gives a possible solution. Then it drives to the best solution step by step. Even if you’re experienced enough, you’ll find that ride very interesting and challenging.
All these aspects make the read smooth, the content very helpful and, as a big bonus, classic jokes from Meyers. My favorite one is from this book:
Modifying our file-reading code to use istreambuf_iterators is so easy, most Visual Basic programmers need no more than two tries to get it right. — Scott Meyers
Final score
Useful for programmers: 10/10
Relevance: 9/10
Real-world examples: 10/10
Detailed and friendly: 10/10
Worth buying: 10/10