C++ STL (Standard Template Library) : Introduction


C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers and so on. C++ supports many different ways of programming like procedural and object-oriented. This makes C++ a very powerful as well as flexible language. That is why we need a library to help us and C++ standard library is one of the most crafted libraries out there.


Standard template library(STL) is at the heart of C++ Standard Library. STL can dramatically change your programming style, allowing your code to be more reusable, readable and robust. STL offers simplicity to make your life efficient. STL is a standardized language and will always be available for use.

Let's discuss in depth now. I suggest you read this article completely to understand everything about why you need STL at the first place.

C++ STL has three major components.

Let's discuss the first two for now which are : containers and algorithms.

In simple terms :

  • containers are used to hold data while.
  • algorithms operate on data held by containers. 

So basically think them as data structures and algorithms.

If you know Object Oriented Programming, you would be knowing that it focusses on encapsulation and tries to combine data and functions(algorithms) into a single capsule called class.

But STL is somewhat opposite to OOP as it separates algorithms and data structures.

What can be advantage of this structure ? 

The advantage of this structure is also the main motivation behind learning STL and that is greater code reusability.

We want one algorithm to work on multiple containers and one container to be able to use different algorithms.

But if we want to apply one algorithm on different containers, we have to provide different implementation for each. This is because all the containers will have different interfaces from each other.

If we think about the number of implementations we would require, then if there are X distinct algorithms and Y distinct containers, then we would require X*Y implementations(each container has separate implementation for each algorithm) which is not desirable and not scalable in today's world.

To solve this problem, we have the third component of STL : iterator.

Each container is required to provide a common interface defined by iterators.

Each iterator can iterate through each item stored in a container.

Now with the help of iterator, the algorithm instead of working on the container works directly only on the iterator.

The algorithm doesn't know anything about the container that it is working on. It only knows about the iterator provided to it.

That will largely improve our level of code reuse.

Instead of X*Y implementations we only need to provide X+Y implementations (one implementation for each algorithm and one implementation required to implement interface of each container).

This structure provided by STL is largely extendible. If we define a new algorithm that operates on iterators then all the existing containers can use that algorithm and similarly if we define a new container which provides an appropriate iterator interface then all the existing algorithms can be applied on that new container.

Also STL is very efficient library and even the modern C++ compilers are optimized for it's code.

STL provides great code readability as they have very meaningful names provided to the containers and algorithms.

So after seeing so many benefits of STL, you might have started understanding the requirement of it in programming.

But remember since STL is a very powerful library, it is not that easy to use and small mistakes in code can lead to significant errors. Therefore one should be very comfortable with STL algorithms and containers to use them efficiently.

This brings us to the end of this article. I feel that you now must have understood why we require to learn STL and why is it so important. 

In the upcoming articles, we would continue with our study of STL by learning everything you require to master STL and write highly efficient and readable code.


Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on WhatsApp or Facebook. 

Happy learning!!