What is STL in C++ , How can we use it ?

Mathanprasannakumar
4 min readMar 22, 2023

--

If you are learning C++ , Congrats you are a genius or can i say you want to stuck with memory management ,pointers ……!!!! just kidding,

Lets see what is STL

STL — Standard Template Library , just like any other library in C++ it will have certain classes and functions for implementing the common Data Structures and algorithms .

It is really beneficial in terms of time and effort as you don’t need to write the full data structure everytime- this will definitely take a lot of time and effort. This library uses a special functionality called Template, hold on we will see what it is down further ,by using the template functionality functions in the library will be generic — it means a block of code or a function that can be used for a variety of data types and structures.

if you don’t want to get into more deep , you can skip the below container part.

This STL contains three components,

  1. Containers
  2. Algorithms
  3. Iterators

Containers::

Containers can be used to store a collection of elements .

In Containers we can further break it down to three types

  1. Simple Containers: Here it will have a simple data structures like vector, pair,forward_list(singly linked list ) , list(doubly linked list).
  2. Container adapters: Here it will have queue , stack , and some priority queue , these are built on top of the simple containers ie, queue, stack and priority queue will have structure related to the simple containers.
  3. Associative containers: These containers are self — balanced binary search trees . mostly set,map, unordered set,unordered map.

Algorithms:

These are the functions that are used to operate on the data structures .

while learning you might have came across find, reverse, sort ,binary_search etc.. these are the functions that are designed to modify or access the containers based on the requirement .

if you want to reverse an array , reverse(arr,arr+n) — this will help you to reverse the all the elements in an array. So like these the above functions are used to modify the containers.

Iterators:

Just like pointers iterators are used to point the element in these containers and we can use the iterators to move through the element in the containers and it is playing a huge role in accessing and manipulating the contents.

It should declared as , if we are accessing vector , vector<int> :: iterator i;

and it can also be declared as

auto i; // this indicates it can point to any datatype

So here i is capable of pointing to the vector elements , now we need to assign it , i = v.begin() this will return the first element address to the iterator , if you put *(dereferance operator)i you can access the value on that address.

v.end() — this will return the address of element after the last element in the vector.

So like these you can have fun with it.

Note :: you cannot call all the iterators as pointers , as if a iterators is pointing to a forward_list then it cannot access the random location in the list like the pointer in arrays . Like these different iterators performs different functions on these containers. follow the image attached and google yourself for a good understanding of the below.

source: GEEKSFORGEEKS

At last what are Templates?

Think that you are having a block of code or a function , that will work for any data types values. for implementing this feature templates are used.

Actually you don’t need to write the same code again just for the sake of different datatype values.

template <typename T>

// Function template
T myMax(T x, T y)
{
return (x > y) ? x : y;
}

// Drivers Method
int main()
{
// This function call creates a function
// where all the T's are replaced with int
cout << myMax<int>(3, 7) << endl;

here template is a key word used to indicate that the function below the statement will have a generic datatype T , in the myMax function , the all parameters are having the type T. This function is applicable for any datatype.Important to note is that while calling you need to mention which datatype you are passing to the function , <int> this indicates the datatype parameter has been passed.

Similarly you can pass the character , string , float values also indicating each datatype for each of the values.

Hope you got a basic understanding about how the STL works . if you want more about STL just upvote this post , so that i will came to know about the interest of the people , i will provide better contents with a deep explanation in a easy way.

--

--

Mathanprasannakumar
0 Followers

Aspiring Computer vision engineer, C++ pointer head, Love to build a AI models for shaping the world.