16.1 — The Standard Template Library (STL)

Congratulations! You made it all the way through the primary portion of the tutorial! In the preceding lessons, we covered all the principal C++ language features (excluding those in the C++11 extension to the language).

So the obvious question is, “what next?”. One thing you’ve probably noticed is that an awful lot of programs use the same concepts over and over again: loops, strings, arrays, sorting, etc… You’ve probably also noticed that writing programs using non-class versions of containers and common algorithms are error-prone. The good news is that C++ comes with a library that is chock full of reusable classes for you to build programs out of. This library is called The C++ Standard Template Library (STL).

Although you have not have known it, you’ve actually been using the STL since your very first program back in lesson 0.6, when you included iostream. iostream (and our friend cout) are part of the STL!

The Standard Template Library

The Standard Template Library is a collection of classes that provide templated containers, algorithms, and iterators. If you need a common class or algorithm, odds are the STL has it. The upside is that you can take advantage of these classes without having to write and debug the classes yourself, and the STL does a good job providing reasonably efficient versions of these classes. The downside is that the STL is complex, and can be a little intimidating since everything is templated.

Fortunately, you can bite off the STL in tiny pieces, using only what you need from it, and ignore the rest until you’re ready to tackle it.

In the next few lessons, we’ll take a high-level look at the types of containers, algorithms, and iterators that the STL provides. Then in subsequent lessons, we’ll dig into some of the specific classes.

16.2 -- STL containers overview
15.6 -- Exception dangers and downsides

5 comments to 16.1 — The Standard Template Library (STL)

  • lishihong108

    I am new here?today is my first to visit here.Please take care of everything.
    Thank you every one!


  • lishihong108

    Hello,everyone!I'm a student of a college from China.I'm glad to see everyone here.I magorde in c ++ programming.I very like it.My English isn't very good.My e-mail is want to make friends with people who speak English.If you interested to me?please leave your e-mail or send a e-mail to me?I very preciate to you,and we can have a chat about c ++ programming or others.I wait your voice.Good luck to everyone!

  • neehar

    Hi Alex,

    I want to find Unique rows and their corresponding indices from a 2d vector. I'm using sort and unique commands from STL, following I use find command in a for-loop to find indices of the unique rows in the original array.

    However, the code is extremely slow compared to Matlab. My array size are quite large around 50000-by-3.

    Is there any way out?

  • kuhlde1337

    Lol. I literally just finished reading the last section of chapter 17. I went to the main page to say "goodbye" when I noticed that something had been uploaded in chapter 16. Glad to see you are back Alex. Also, does this mean that c++0x is out?

    • C++0x is now called C++11 (since it wasn’t finished last decade) and it has finally been approved. I do plan on getting around to adding some tutorials about the it, but I thought I’d do a few on the STL first since that’s probably more immediately applicable to most people. Compiler support for C++11 is rather spotty (see

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">