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).

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.1 -- Intro to smart pointers and move semantics

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

  • 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

  • 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?

  • 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!

  • lishihong108

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


  • C++ newbie

    Woooooooooooot!  The official end!  From hello world to the end!

    I recommend you teach about vectors in the array portion. They are so much better

    • Alex

      Congrats on making it through!

      I was thinking about adding vectors to the array section the other day. The challenge with teaching vectors is that they use templates, which I don’t cover until chapter 14.

      But maybe it’s possible to do without fully explaining what templates are.

  • Quang

    Hi Alex, I haven’t reached this lesson yet, but I want to ask you something that has been stuck in my mind for weeks.
    After I finish this tutorial, I believe I still have so much thing to learn, but I realize that I am not ready at C++. The point is I have participated in a lot of forums recently like Stackoverflow, Codeguru, Dreamincode, Spoj,….. but I find myself nowhere among them, I couldn’t help them to solve the problems, I couldn’t read their code because it was slightly different from yours.I believe the problem is I have so much theories but just a little experience in coding (although i rewrite your example code a lot). I just want to ask you how can I improve my coding and the capability of reading and understanding other’s code. And where I can find a lot of exercises to practice. And after I finish C++, which I should learn first: framework, database, UI, web, or other language,…? By the way I’m 17 years old and I’m a little bit worrying about my career, am I too old to be a good programmer?? Thank you for your time Alex!!

    • Devashish

      What’s up Quang! I think I can answer you.

      >> After I finish this tutorial, I believe I still have so much thing to learn, but I realize that I am not ready at C++.

      Yes, definitely you need to learn much more things. Well, as far as I know, no body can master each portion of a language like C++. There is a huge list of libraries, each one comes with its own set of rules and designed to help you build something without worrying about everything. The question here is, why would you need to know everything. Just focus on tools you will need to work with. For example, if you want to build a scientific calculator, learning how to work with sound libraries is not required. Sure that would be a + point, but you are not forced to learn how to do stuffs with sound libraries, at least for building calculator of a kind.

      >> I have participated in a lot of forums recently like Stackoverflow, Codeguru, Dreamincode, Spoj,….. but I find myself nowhere among them, I couldn’t help them to solve the problems, I couldn’t read their code because it was slightly different from yours

      If you just started to learn a language, you can’t write dictionaries 🙂 No one can expect an expert solution from a beginner. Stack Overflow connects millions of programmers. The answer you see are posted by experts, who faced the same problem years before. If you are just started, don’t try to answer people on overflow, just make sure your questions are smart enough to attract people to answer and upvote. If I can’t read a piece of code, I don’t give much time to understand it. That’s because I know it’s out of scope for now. Alex’s examples are written in a way that are easy to understand for everyone. For example, Quicksort is much faster than selection sort that is used to explain how sorting works in a section. Alex could have picked Quicksort for his example, but that’s not for beginners like us. (Personal advice: don’t be too quick when learning. Take time, practice and solve some problems using it. Trust me, practice generates questions 🙂 )

      >> where I can find a lot of exercises to practice
      If you want to challenge yourself, check out Projecteuler, HackerRank Codechef etc. Keep in mind that problems available in these sites are tricky enough to solve. First of all, forget about performance and look if you can solve a problem, if you can then try to solve it in a more efficient way. If you are thinking that your final solution is faster than any other solution in the world, you will be amazed after seeing that it takes 5 seconds with your code to be solved, and there is a solution exist that is doing the same job in 5 mili-seconds, have less than 20 lines, and is going out of your head. That’s really normal with us. Don’t forget that those solutions are designed by experts. Coders with same level as yours and mine can’t do this (Assuming they are not alliens). Just be happy that you can at least solve it. It’s not guaranteed that everyone can solve all the problems present on those sites. They write to challenge, not to just let others play with them.

      >>And after I finish C++, which I should learn first: framework, database, UI, web, or other language,…?

      It really depends on what do you want to build. A desktop app? Then learn QT and components yo will need to write your program.

      Don’t learn to earn something out of C++, learn it to build something that make things easy for people. And yes you are not too old. I am also of 17 and just 3 months old in C++. Don’t stop learning. I am not an expert, just sharing what makes me feel like a good beginner. I enjoy what I learn. You shold too. 🙂

      • Quang

        Hey bro, can’t believe you’re just like me 17 yo and 3 months in C++.But I think you might/may have learnt another language(s) before C++ because I think you’re really good at it. Anyway thank for your websites, I’ll try my best at there. Cheers!! xD

    • Alex

      Hi Quang. Good questions.
      1) Writing code is as much of an art as it is a science -- people have all different kinds of techniques and styles. You may find some techniques and styles more challenging than other, and that’s normal. Just keep reading and working to understand. Ask lots of questions. And just as importantly, find challenging projects for yourself. The best way to learn is to do -- to run into problems, and then overcome them.
      2) I’m not aware of a good source for exercises. I’ve always assigned myself projects. What are you interested in? If games, write a game. If finance, write a stock predictor. If graphics, learn some OpenGL. Start simple, and grow it over time. You’ll learn a ton by doing things wrong the first time and having to go back and redo them.
      3) After C++, the next thing I would study is algorithms. Things like sorting, linked lists, maps, etc… (all of which are implemented in the STL). Those will expand your toolkit considerably and allow you to tackle bigger challenges.
      4) You are definitely not too old. Many people don’t start programming until they are college age in the US (~18) and they go on to have good careers.

      • Quang

        Hi Alex! Thank you for your advice and your time. I will try to learn algorithms after I nearly finish C++. The point is where can I find a project because I think the best way to learn C++ is to have a group/team and do it together so that you can learn from them, but it turns out none of my friends interested in program, so i’m kinda lonely here!. So thats why i have to search for online exercises and participate in forum. But big thank for your advice, it really helps me out xD!

        • Alex

          I never had any group projects to work on either (until I got to college), so I just made up my own challenges. I wrote a lot of unfinished games. 🙂

    • Darren

      Good god. Imagine thinking at 17 that you’re too old to be a "good" programmer, come to think of it imagine thinking at 17 you’re too old for anything! Wow, talk about putting pressure on yourself. You’re barely out of diapers at 17 and the world’s your lobster. I’m 36 and only within the passed 2-3 years have I seriously committed to learning C++. I’ve dabbled here and there in Fortran, C, Basic, Pascal (any one remember that?) and more recently looked into Python, Ruby, and Java. I’ve found the best why to learn is to put the theory on the back burner, roll-up your sleeves, and get your hands dirty. Set yourself a programming project and get stuck in. Write a "Moon Lander" game that simulates gravity, make a program to tell you how many days it is till your next birthday from the current date, write a text adventure game, make a fictional banking program where you can deposit, withdraw, and merge bank accounts. Although I detest this phrase it is appropriate; the only limit is your imagination.

      17 and worrying about a career <head-shake> - at 17 I was more worried about my spotty face and had an unhealthy obsession with Baywatch.

  • codingtony

    Hi Quang!
    1、there are many sites about coding. Like github. you can create a project share to the world, and people from the world can take part in. and you can check out others project.
    2、you can search opensource in google and there are many good project too.
    3、in fact, the best is to create a project you love, no matter it is small or big. you can test all your learn, and meet many problems, finally solve them. the experience is better than theory.

  • Denis

    Done with C++? Start OpenCV C++ Tutorial:

  • Himanshu

    Thankyou for such nice tutorials. I appreciate your efforts.
    My question is, are you planning to write some lessons on data structures and algorithms using c++?
    If yes, how much time it would take you to do so?
    because i like how you explain topics and i havent found any other site this effective. So, i am really looking forward to study data structures: linked lists, trees and queues here.

    • Alex

      I do intend to cover these topics (eventually). But it could take me a while to get there, depending on where it ultimately slots into the existing lesson structure. I still have a lot of other core language mechanics that I want to write about, like move semantics, and it seems like maybe I should do those first.

  • Mauricio Mirabetti

    Alex, I’ve followed your tutorial so far (it’s pretty good, of course), and I’ll keep reading through (16,17, 18, and appendices, as they are now).

    My question is: is there a way to "subscribe" to your whole tutorial material and be warned, for example, when a specific page gets updated or a new chapter is created?

    Thanks a bunch.


  • wannebeprogrammer

    Hello Alex, first I would like to thank u for those tutorials, I really appreciate.

    I would like to make a simple game like tic tac toe and snake but unfortunately I don’t know how to begin with. Is it normal to look at others code and learn from it. How other people do it?

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter