DEV Community

Naweli Verma
Naweli Verma

Posted on

Standard Template Library aka. STL

Programmers :- What is this STL library?

STL :- Try to know about me, I know you will end up loving me 😚

PS: I am not going to complicate things, I will write only which is required to use it, rather than scaring you with super geeky things

Okay so as the name suggests Standard Template Library it is a simple library of templates of some really cool and useful things, it gives you power to implement things without implementing those things metadata πŸ’₯

It is like cuppa noodles, where you only need to add the hot boiling water and boom you are done.

What are those things, which makes it so useful and cool. Let's go to explore.

So, STL has four components which gives this much power to it.

  • Containers
    (These are the boxes to store our data)

  • Algorithms
    (They are prebuild methods and function like sort(), search()
    which act on containers. Yeah you don't need to write whole logic
    of sort or any other method if you are using this library, it
    gives you prebuild template, remember)

  • Function

  • Iterators
    (They are used for iterating the data in sequential manner)

Don't get Panic ❗

Here we will learn everything super easily which will definitely help you to start by your own.

Containers

Containers are nothing but a box, which stores your data. STL has 4 types of boxes or containers. Those boxes has their own varieties. Let's check those boxes and their varieties out!

->1 . Sequential Containers
(Pretty self explanatory, it stores data in a sequential manners, you know this behavior from Arrays)

-> Vectors
-> List
-> Deques
-> Arrays
-> Forward List

->2. Container Adaptors
(It is similar like sequential containers, the interface is only different)

-> Queue
-> Priority Queue
-> Stacks

->3. Associative Container
(Works awesome when you have sorted data, gives o(nlogn) complexity)

-> Map
-> Multimap
-> Sets
-> Multisets

->4. Unordered Associative Container
(Ditto like associative containers but we use it when we have unsorted data)

-> Unordered Map
-> Unordered Multimap
-> Unordered Sets
-> Unordered Multisets

Yeah this is it, I know you must be thinking, what is this πŸ˜• but,

Trust the process my dear co-programmers πŸ’–

Now lets know them deeply

πŸŽƒ Sequential Containers

Vectors

They are contiguous containers which handles data dynamically. It resizes itself when you do any operation like delete, insert or anything. You don't need to predefine the size like we do in Arrays.

Functions from Algorithms component we can use on vectors are

begin() – Returns an iterator pointing to the first element in the vector

end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector

sort(arr.begin(), arr.end()) - sorts the array
etc;

There are a lot of functions which you can use according to your need.

You can initialize a vector like this.

vector<int> vector_name;

vector <pair,<int,int>> vector_name;
Enter fullscreen mode Exit fullscreen mode

Lists

They are sequential components for non-contiguous data. It allows us to pop and push from back and front. It stores only one data a block. Very much like doubly linked list. List keeps track of both the next and previous elements

list<int> list_name;

Enter fullscreen mode Exit fullscreen mode

Google about it's methods.

Deques

They are doubly ended queues. It allows contradiction and expansion form both end.

Now you must be thinking, this is same like list, right?

So, the answer for that is No, because it list stores only one data in a single memory block whereas deques stores multiple element in a single memory block.

deque<int> deque_name;

Enter fullscreen mode Exit fullscreen mode

I'm assuming you already know about Arrays, so I am skipping it.

Forward Lists

They are also sequential components for non-contiguous data. It stores only one data a block. Very much like singly linked list. Forward list keeps track of the location of only the next element, not the previous element.

forward_list<int> forwardList_name;

Enter fullscreen mode Exit fullscreen mode

πŸŽƒ Containers Adaptors

Stacks

It is pretty awesome, it is like when you put things on another. Like a deck of cards resting on your table or the stacks of plates in your kitchen. If you want to take the second plate from the bottom you need to remove the other plates that are resting on top of your desired plate.

So it shows a LIFO behavior - Last in, First Out
It won't allow you to access the element directly, you need to access top elements, means traverse sequentially only.

Some cool operations are pop(), push()

stack<int> stack_name;

Enter fullscreen mode Exit fullscreen mode

Queue

The line where you stand, when you want to book some Marvel movie tickets πŸŽ₯ or when you are waiting in the line of your favorite resturent. That is queue. Elements are inserted at the back (end) and are deleted from the front

"Who goes first, comes out first" - FIFO - First in, First Out

queue<int> queue_name;

Enter fullscreen mode Exit fullscreen mode

Priority Queues

It uses the property of Stacks and Queues, and stores data in sorted non-increasing order. It gives priority to the greatest element. So suppose if you insert 20,6,99,77,0 to this it will store it like 99,77,20,6,0

priority_queue<int>priorityQueue_name 
//default, stores data in descending order - max heap

priority_queue <int, vector<int>, greater<int>> g = gq;  
//greater<int> is comparator here, for ascending order -min heap
Enter fullscreen mode Exit fullscreen mode

_Ahh!! I love these associative and unordered associative containers πŸ’— _

πŸŽƒ Associative Containers

The time complexity of every associative containers is O(nlogn)

Map

So Map is more like a object in CPP. Yes, come on read with me.

Map stores sorted data in key and value pair, same like objects does. It gets sorted by comparing the keys.
It never allow you to store duplicate datas. The whole data in map is identical to each others.

map<int,int> map_name;

map<pair<int,int>,int>map_name;

Enter fullscreen mode Exit fullscreen mode

Multimap

It is ditto like map, but the only which makes it different from Map is that,

Multimap can stores duplicate values.

Set

It stores data like Arrays but the thing which makes it unique is that it stores data in sorted way and you can't store any duplicate element in set.

set<int>set_name;
Enter fullscreen mode Exit fullscreen mode

Multiset

It is ditto like Set, but the only which makes it different from Set is that,
Multiset can stores duplicate values.


πŸŽƒ Unordered Associative Containers

Unordered Map

It is same as map, It stores unique data in key value pair but as the name suggests unordered, it doesn't stores data in sorted manner.

unordered_map<int,int> unorderedMap_name;

unordered_map<pair<int,int>,int>unorderedMap_name;

Enter fullscreen mode Exit fullscreen mode

Unordered Multimap

It is same as Multimap, It stores data in key value pair but as the name suggests unordered, it doesn't stores data in sorted manner and it also allows to store duplicate elements.

Unordered Set

It is same as Set, It stores unique data element, but as the name suggests unordered, it doesn't stores data in sorted manner. It uses hashing (I will explain about hashing in some other post) , the time complexity of this is O(1).

unordered_set<int> unorderedSet_name;

Enter fullscreen mode Exit fullscreen mode

Unordered Multiset

It is same as Multiset, as the name suggests unordered, it doesn't stores data in sorted manner and it also allows to store duplicate elements.

Phew, this is it
This much information is sufficient to start, you can read about the STL algorithms in my next post.

Top comments (0)