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
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
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
**The output of this will be 5 0`s we can replace 0's with strings,integers etc
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
**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
*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
In pairs values can be assigned as shown below
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
As you have noticed iterator is used to traverse through aset**
Sets wont contain duplicates
Although we are inserting 100 times the value 1 to the set it will only contain a single '1'
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.Map ensures that at most one pair with specific key exists.It also has [] operator defined.
Its under the map header.
Top comments (1)
Thanks Victor!