What is STL in C++?


STL is a standard template library in C++ that offers powerful, adaptable, and reusable generic classes and functions.

  • Powerful: Consider vector for example. Clearly vector that is a part of STL is more powerful than a normal array.
  • Reusable: STL can hold or work with any type of primitive or user-defined types.
  • Adaptable: It provides containers, iterators, and algorithms that we can customize to do whatever we need to do.

In short, STL is a library of components that are type-safe, extensible, consistent, fast with a fixed time, and size complexity.

Components of STL

The components in STL are classified into three categories:

  1. Containers
  2. Algorithms
  3. Iterators


Containers in STL is something that can hold values or data. The data could be the collections of objects or primitive types.

Examples of some containers are array, vector, deque, map, stack etc.

Types of Containers:

  • Sequence containers: These are the containers that maintain the ordering of the data stored in them. For example, array, vector, list, deque, etc.
  • Associative containers: These type of containers stores data in predefined order or no order at all. For example, set, map, etc.
  • Container adapters: These are the variations of other containers that not support iterators, thus cannot be used with algorithms. Example of such containers are stack, queue, etc.


Algorithms in STL are functions that process sequences of elements in the containers.

They are often used on containers for accomplishing some task or output.

Some examples of STL algorithms are find, max, count, sort etc.


Iterators in STL generate sequences of elements from containers that the algorithm uses.

In short, they help in iterating through the elements of the containers.

Some examples of STL iterators are forward, reverse etc.

Types of Iterators:

  • Input Iterators: This type of iterators makes container elements available to the program.
  • Output Iterators: These iterators iterate over a sequence and write it into the container.
  • Forward Iterators: This type of iterators navigate one item at a time in one direction.
  • Bi-directional Iterators: This type of iterators navigate one item at a time in both directions.
  • Random Access Iterators: These iterators use subscript operator to directly access the container elements.

All three components of STL are independent of each other yet they work efficiently with each other.

C++ Example: STL

We will definitely discuss each component of STL separately in depth, but for now, let’s look at a simple example to get a clear understanding of all forms of STL.

#include <iostream>
#include <vector>        //Container
#include <iterator>      //Iterator
#include <algorithm>     //Algorithm
using namespace std;
int main()
    //store element in the container
    vector<int> num_list= {2, 6, 3, 8};
    //sort elements using thesort(<start>,<end>) algorithm function
    sort(num_list.begin(), num_list.end());
    //define an iterator to iterate through vector elements
    vector<int>::iterator it;
    //iterate through vector elements and output them
    for(it=num_list.begin(); it != num_list.end(); it++){
        cout << *it << " ";
    return 0;


sorted int stl vector output

In this tutorial, we got to know what STL is in C++, what its types are and we also saw an example using STL components in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *