STL(Standard Template Library)
Tags: programming, cpp, anonymous, codenewbie
Published: 2020-07-03 21:24:08

**Hello fellow devs,
Learning STL is really important for interviews as well as competitive programming in this post we will cover some basic STL🐿.**

⏳Motivation-why should we apply long algorithms when c++ provides it in one line  of code.

⌛Goal-Learn the basics of STL

**So without much prelude lets  start our STL journey.
This will be the first post in our journey.
So lets start with containers,there are several STL containers like vectors,pairs,maps etc.**
##*1)VECTORS*
**In c++ most of us are familiar with arrays but not with vectors,vectors are nothing but dynamic arrays.**

**For instance consider we are creating an array of size 'n' but after some lines of code we realise that we need a larger array so we copy the values of the previous array into a larger one of a large size say '4n' imagine this happening again and again what will you do,no worries we have got  🐭vectors🐺.**

**A vector is dynamically sized that is it increases its size by itself when the value to be stored increases**

**The below picture shows the code**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8pqrfyzi8pzaap18oeyu.png)

**Please dont forget to include vector**

**Now lets try printing the values in it**
**For printing the values we can use for loops as well as iterators**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x9gzqobwo6gnuwme0kc6.png)

**This will produce an empty result as we have not added anything to the vector**
**but whatif we want to create a vector with some default values,yes we can do that too**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0u5ev8lb486dsx6svo2e.png)
**The output of this will be 5 0`s we can replace 0's with strings,integers etc
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n535cx38ktqflqznekn7.png)

**To add elements to a vector push_back(value) method is used usually a macro pb is defined for pushback in competitons to save time**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mwovjti5h5zc3awtxlu8.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/347mo78gvte245zx1x61.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/32iq7htqqatlfgjto1br.png)
**The above code also produces the same output.

###Always keep in mind,when vector is passed as a  parameter to a function a copy is created.It may take a lot of time and memory to create a new vectors,so never give vector as a parameter,but if there is no other option give like shown below
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v053yo5d8kfplprwrotn.png)

**Other features provided by vector includes **

1]vi.size()
2]vi.empty()
3]vi.resize()
4]vi.clear()
5]vi.erase()
try these out by yourselves.

#*2)PAIRS*
**As the name suggests pairs are use to store pairs of values**
**The code snippet given below demonstrates the use of pairs to store 2 integers**
**Pairs is under the *utility* header**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1smulj6tbsmngfznzgrv.png)

**In pairs values can be assigned as shown below**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wy1ey5mhdl3mn76rckxr.png)

**Both of the above code snippets produce the same output ****'3'.**
**make_pair(values) is used to give values explicitly**

**To swap two pairs swap function is used**
**_pair1.swap(pair2)_**

#*3)SET*

**Consider we need a container with the following features **
1]add an element but no duplicates.
2]remove elements
3]get count
4]check whether elements are present

**c++ provides 'set' for this purpose**
**It is under the 'set' header**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cllkg5x5byhhlefqejr0.png)

**As you have noticed iterator is used to traverse through a**set**
**Sets wont contain duplicates**
**Although we are inserting 100 times the value 1 to the set it will only contain a single '1'**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/du2a1xcphc1diesu7v6e.png)

**Set also has a find element which  searches for an element in O(logn)time complexity.**
**if the element is not present the end() iterator is returned.**
** Dont confuse this with the normal find function which locates an element in O(n)time complexity **

##*4)MAPS*
This is the last container we will be discussing about in this note 
Actually map is verymuch like set ,except it contains not just values but pairs<Key,Value>.Map ensures that at most one pair with specific key exists.It also has [] operator defined.

Its under the map header.