tags:

views:

462

answers:

8

I'm learning C++ and the book I'm reading (The C++ Programming Language) says to not reinvent the wheel, to rely on the standard libraries. In C, I often end up creating a linked list, and link list iteration over and over again (maybe I'm doing that wrong not sure), so the ideas of containers available in C++, and strings, and algorithms really appeal to me. However I have read a little online, and heard some criticisms from my friends and coworkers about STL, so I thought I maybe I'd pick some brains here.

What are some best practices for using STL, and what lessons have you learned about STL?

+9  A: 

You might want to pick up a copy of "Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition)":

http://www.amazon.com/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889

I've found it to be invaluable, and it's still very relevant today, even if you aren't programming in C++.

casperOne
Perhaps "effective STL" by the same author would be more to the point?
anon
Reading all 3 of his C++ books is definitely a best practice.
twk
Awesome. I will look at these books. :)
apphacker
you should probably get 3rd edition of effective C++...
+1 for Effective C++. That is the most important book on C++ IMO.
rlbond
+6  A: 

If you really want to learn the C++ Standard Library (which includes things like strings, which have not traditionally been seen as part of the STL), you need a good book. The best one in this area is "The C++ Standard Library" by Nicolai Jossutis.

anon
+1  A: 

You should understand the concept of template, and other polymorphism, in order to efficiently use the STL.

Jérôme
+1  A: 

to learn about STL you need to understand templates and also you should be good in data structures.

+2  A: 

Why don't you tell us those criticisms, and we'll respond? If the criticisms are valid, we'll tell you that. And if they're not, we'll tell you why not.

The STL has a mixed history, because initially, 1) few people understood it, and 2) few compilers implemented it correctly. But that was a decade ago. Today? It works. It's efficient. It solves a lot of problems. The biggest problem with it is that it takes some time to wrap your head around how it works.

The simplest best practice is "Use the STL whenever it offers functionality that you need". And it's hard to offer more specific advice unless we know what criticisms it's up against.

But in general, it's typically the case that people who criticize it are simply not C++ programmers. C programmers who have learned to use classes fall into this category.

jalf
+13  A: 

There is a companion book to the Effective C++ series, which is called "Effective STL". It's a good starting point for learning about best practises using the Standard C++ library (neé STL).

Timo Geusch
+5  A: 

The only cases I can think of off the top of my head when the SC++L is not appropriate to use are some rare situations in which either a proper implementation is not provided (perhaps you're working on some obscure platform for which only limited C++ compilers have implemented) or extreme performance is required (perhaps for code that exists at the core of a graphics rendering system for next-generation games).

If you're using an ordinary computer, it's 99% certain that you're not in the first case. As for the second case, you should absolutely only consider implementing your own set of containers and algorithms for performance reasons if you have definitive evidence from good profiling tools that the bottleneck in your program is the SC++L.

The best practice regarding the SC++L is to simply use it whenever possible. In addition, almost all modern C++ code makes heavy use of Boost, which you can think of as an excellent and massive extension to the SC++L. Whenever you find yourself wanting to do a fairly standard algorithmic task, you should use Google to see if either the SC++L or Boost provide premade, tested, proven facilities for accomplishing this task.

Zach Conn
+2  A: 

The STL was written by the best brains. You probably won't come up with better implementation than that in most cases. Its performance is good, it's bug free, and it's a good standard for passing parameters between methods, APIs, code components, and needless to say, it encapsulates all the ugly stuff. The thing is, you have to know how to choose the right container for your problem. Otherwise, you might not enjoy its benefits. There are some articles on the web regarding how to pick the right STL container. One good link is: STL Containers , and it has a nice flow chart of how to pick your container.

Gal Goldman