How learning STL in c++ can help you code faster , shorter and simple?
All you need to know about C++ STL is here!
After you have finished c++ basics and wondering what to do next then the best choice from my point of view is to start learning c++ STL .
Are you tired of writing big codes and just want to use functions directly like you use to do in strings in c ?
Along with a STL you also get many direct functions. Using STL for different data storage containers or data structures like arrays, stacks, queues, lists (both singly linked list and doubly linked list) etc, you do not have to write big and lengthy codes for creating them, inserting elements into them and a whole lot number of other operations. STLs are also really really helpful in competitive programming.
NOTE : Prior to doing STL, you should learn about templates in c++ which comes under objective c++. But since you have to use direct functions, that's why not that much deep knowledge is required. If you can remember the syntax of a given function, that's fine also.
What is STL (not going into too much deep)?
STL stands for Standard template library. It is made up of 4 parts. These are:
1. Algorithms
2. Iterators
3. Functions
4. Containers
Algorithms :
You have heard a lot about algorithms already. They form the logic of our code. The example for algorithms are sorting algorithm , searching algorithm etc. You can write algorithm for any program.
Containers :
As the word sounds , these are the containers which stores the data. For example - arrays , stacks , queues , lists etc. We will learn about some new containers when we will learn STL .
Iterators :
An iterator is something which helps in moving within the container . Iterator points to a specific element and it moves according to our commands from one element to another.
Functions :
These are the direct methods to call a defined and specific program to complete a certain task. For example - stack_name.pop() will delete the top element from the stack .
What are some important STLs you should learn?
There are a lot of STLs available in c++. But the most important ones are -
vector, set, multiset, map, multimap, list, forward_list, queue, dequeue, stack, priority_queue, unordered_set, unordered_multiset, unordered_map and unordered_multimap.
If you master these, you can even win a competition easily as you know how to code quickly. Since the functions defined in the STL are well tested by the c++ developers, they are very very efficient. Some STLs provide really a fast way for removal and insertion of elements and other operations also.
What are the libraries you need to include to use STLs ?
You can use this single library which consists of all the libraries in c++ i.e #include<bits/stdc++.h> .
But if you don't want to use it and rather prefer to use all libraries one by one then their is a different library made for each STL which you need to include before using the STL . For example - we need to use #include<vector> to use all the vector functions. Similarly for each STL , there is a defined library to use the functions related to that particular STL.
Where can you practice STL related questions?
You can go to hackerearth and try to solve every problem in the data structures problem section using STL. I am currently doing all the given problems of the above section using STL only, once i have finished i will share my solutions. If you go to the hackerrank site, there in c++ practice zone check the STL sub domain and start doing the problems. You can also find some of the solutions on my blog that are done using STL . You can always use vectors instead of arrays to practice vector. Also when you finish a problem using simple code try to find out different solutions for that problem in the discussion section and there you can ask or find a solution using c++ STL .
Here's is an example how using STL can save your time and also can make your program more efficient:
STL are not only made for implementing data structures and using their operations easily but alsp you can use them in general programming . See the example below -
Suppose we are given an array with repeated elements and we have to find the frequency of each element in the array. We will be using map STL here to make the problem easier. First i will do the problem using general approach and then using map STL.
General approach:
Time complexity - O(n^2)
Therefore, for large test cases this code will not work.
Using map STL:
Time complexity - O(n)
So you can see easily how STL in c++ can help you code faster , shorter and simple.
Guys , if you have any queries or need more explanation for something , comment below!
Comments
Post a Comment