hey everyone welcome back to take you forward so in this video we are going to learn about c plus stl what is stl stl is basically standard template library now in order to write codes in c plus plus assume you're you're going to use a container or an algorithm a lot of times what happens is you don't need to pre-define the container or write the long long codes for that container or for an algorithm so stl is basically a compilation of algorithms containers iterators functions in a minimized version so that you don't have to write lengthy lines of code and you can use that stl and you can get access to a container or to an algorithm so in this video i will be teaching you all the stl that is actually required to get started with dsa so please make sure you watch this video till the end to understand or to learn c plus plus stl in depth so without uh wasting any time let's get started so let's understand the skeleton of a c plus plus code so you can see this is the entire skeleton of a code and the first line is hash include bits stdc plus plus dot h generally uh this is nothing but a library and you would have learned about something like math dot h so in order to include all the algorithms in math library you have to hash include math dot similarly in order to include all the libraries in string you have to give string dot h similarly if you are wanting to add anything you have to actually write hash include the library.h name for sure now imagine you are writing an algorithm or a program where you require a bunch of libraries so you cannot actually waste a lot of time in including all the libraries individually so what c plus plus tells us okay hey listen i have all the libraries in inside this stdc plus dot h so just include this one and all the libraries are automatically included and i don't need to individually add them now this is the main where you actually write the entire lines of code now what is this actually for using namespace std now imagine just for a case imagine i write scene of a and a is an integer so this works but if you just omit this line this will actually give error now if you don't add this line you actually have to write std double colon scene of a or std double colon c out of a this basically takes an input into a this basically prints the a into the screen so if you don't write using namespace std this you have to write every time so that's a again hectic process so that is the reason what we do is we write using namespace std and we omit this but if you want to know about more in depth you can definitely read a lot of articles i'll be leaving a link of an article in the description below you can check that out so before moving to the stl part let's understand functions so there are a couple of functions one is the void so if you write void and probably print and over here you give c out let's say my name raj and i call the print function so what happens is this print function calls and this will output raj in the screen now this is a void function what does a void function means it will not return you anything okay so that is one kind of function and the other kind of function is a return type function assume i write int of sum a int of b and i say can you please return a plus b and over here i say int s equal to sum can you just do a sum of one and b so what happens is it calls this function passes one into a passes five into b and returns a plus b yes and it returns a plus b so you get a plus b that is basically 1 plus 5 6 into the s and now if you do a c out of s what happens is this actually prints 6 into the screen you need to understand this now this is a return type function right now this can be a double this can be anything like you can use any data type as you wish to so these are the basic stuffs about the c plus plus skeleton of record okay so before moving to the next part of the video i'd love to thank the sponsor of this video which is coding ninjas according ninjas is india's now why coding ninjas because engage your courses well structured so learning and also the courses are very well curated because these courses are prepared by people who have been at iit's as well as stanford and by the people who have been at amazon facebook and google now if you don't believe it you can check out the facebook as well as the google rating of coding ninjas the best thing about them that i find personally is the doubt resolution time like the average doubt resolution time in the last one year has been 10 minutes like if you're raising it out it gets solved within 10 minutes so that's the amazing that's the most amazing thing that they do provide so if you are looking out to buy any of the courses you can check out the link in the description you can easily get an additional 20 discount on whatever price that has been going around so guys make sure you check out coding ninjas the link will be in the description now it's time that we move into the actual part that is the c plus stl so you need to understand that c plus plus stl is divided into four parts the first being algorithms the second being containers the third being functions the fourth being i traders so as of now we will be learning about containers it can be vector it can be queue set map and a lot of other things and we during the course of learning containers i will be also teaching you what are iterators so these couple of things we'll be learning at first after that i'll be talking about different algorithms and different functions that do exist in c plus plus stl so before moving on to containers you have to actually learn about pairs now what is pairs pairs is a part of utility library okay now imagine i say that i want to store couple of integers like on a store one and i want to store three so the only way that you can do is you can store it in a pair now how is pair defined you define the pair stuff and then you see the first step that you want to store is of integer data type the second step that you want to store is of integer data type then you enclose them into curly braces so what happens is this actually stores everything into a variable p so this variable p is now having one comma three that is the meaning of this particular line so that's how you define now in place of this int you can also have something like double string character the data type can be anything okay now if p is storing something like one comma three now if i'm accessing p it's actually a pair what if i want to access this one what if i want to access this three so it's very simple what you say is you just write p dot first p dot first and that will go and access this particular one and if you write p dot second that will go and access this particular three so that is as simple as it can get so if you are printing p p dot first one gets printed if you are printing p dot second three gets printed i hope uh this is clear now imagine you are wanting to store so as of now you know that if you declare an integer data type or a string data type or a character data type you can always store a single variable like if i want to store int a equal to 2 that's fine and i also know if i want to store two variables i can use something like pair but what if the question comes up and they see that let's store three variables four variables five variables can you do it yes we can do it we can use the nested property of pair yes we can use the nested property of pay i imagine i say you that you're going to store one three four so what you'll see is okay i have one pair and i know this pair can store two guys so this is the first guy and the second guy in a pair so what i'll say is the second guy can be of a paired data type thereby the second guy stores two guys in itself that is how you can do it if you're wanting to store three variables got it like pair of integer then in the second guy you say that i'm going to store a pair in itself so basically one comma and the second guy is a pair so again a curly brace and three comma four so that's how you can also store three and eventually if they're asking you to store four variables you can go nested nested nested nested that you can improvise okay so that's how you actually store more than two variables in a pair but now if you want to access this one now if you remember well enough i told you pair contains two guys right and this guy was called first if you remember well enough this guy was called first and this guy was called second correct now but this second guy now stores two guys so can i say this guy is nothing but first you get second then seconds first so seconds first is what this particular guy is and what is this guy this is nothing but the second dot second so this is second or second as simple as that so this is how you can easily access so if i ask you what is p dot first it's one if i ask you what is p dot second or second it's actually four if i ask you p dot second dot first it's actually three this will be the output if i write c out okay now what if i declare now as of now you would have been declaring arrays like integer array right or character array or something like a string array but can i declare a pair array yes you can declare a pair array the data type can be anything pair can also be data type now this is the index zero this is the index one this is the index two now so you are storing pairs in your array indexes as of now so pair can be treated as a data type it generally lies inside the utility library yes it lies inside the utility library okay now if i'm if i'm trying to access array1 so that's basically this dot second now of this guy which is the second that's five so if i'm trying to print this this is going to print five so this is the entire knowledge of pair that is required in order to get started with data structures and algorithms and the next part we'll be learning about vectors so the first container that we will be learning is vectors please understand every possible function about vectors because these functions will be similar in all the other containers like q list map set so please please make sure you understand all the functions in vector in depth so till now if i ask you to store five values the normal thing you would have done was you would have declared an array of size five and that will be giving you access to five possible indexes right if you remember well enough this gives access to five possible indexes the first one being zero the second one being one two three four now but afterwards if you want to modify it like if i want that i want to enter one more element i cannot modify the size of this array because this array has been declared of size 5 and i cannot modify the size because arrays are constant in size so this is where something like vector comes in vector is a container which is dynamic in nature like you can always increase the size of the vector whenever you wish to so if there is a requirement where you do not know the size of a particular data structure that will be required that's when you think of a vector and that is the best place to use vector so vector is a container only okay which stores elements in a similar fashion as the array does okay now in order to declare vector it's very simple you just give the vector name then you give the data type it can be integer double cache string anything and then you declare the data type name like over here it's v it can be large it can be striver it can be vec it can be abcd it can be anything okay so right after that there's a function as push back so if you're saying pushback of one what it does is this line basically does is it creates an empty container remember this it creates an empty container in the next line it says pushback of one so this empty container says okay i'm empty so i'm gonna take one into it so pushback of one will do this i'm gonna take one into it now there is another function as in place back what do you mean by employees back it's a similar to pushback it is similar to pushback so the moment you do in place back to it dynamically increases its size and inserts two at the back it dynamically increases its size and pushes two in the back now generically m plays back is faster than pushback now you can find the reasoning why and quora i'll be leaving the link in the description okay now this is how you can define vector now can we define vector of a paired data type you can again define vector of a data type as i said so what you just need to do is you need to change the data type declaration inside this so if you just change it you can always but over here there is a trick as i said there are a couple of ways in which you can insert elements into vector one was push back the other one was in place back so if you're using push back you have to insert like one comma two you have to give the curly braces in order to enter like in order to insert a particular pair but if you're using m place back and you write without curly braces one comma two and plays back automatically assumes it to be a pair and takes it as an input and inserts into the vector that you have defined so that is how in place back is different from pushback in terms of syntax now what if i want to declare a container with a lot of elements already filled so imagine this is the size now this is how you can also declare something of a size like this is the size that you can give so what happens is a container containing hundred like containing five instances of hundred is already defined it's a container containing five instances of hundred is already defined where this is the zeroth index this is the first index this is the second index this is the third index and this is the fourth index so container of size five is already defined with five instances of hundred what if i don't want to declare with hundred you can just declare of this if you do this what happens is a container of size five with five instances of zero or any garbage value is declared now this depends on the compiler okay that's how you can easily do it so similarly if i uh do something like v1 of 520 this will uh declare a container of five instances of 20 okay now what if i want to copy this container into some other vector so you just need to declare another vector v2 and you need to pass on this particular vector v1 so v2 will be this similar container but a copy of it like a copy of it not the same one it will be another container of five instances of 20 so this is how generically the declaration is if you know these kind of declarations it does work you don't need to declare any further you might be thinking but trevor what we what if we define the size of the vector to be five can we increase it afterwards yes even after this you try this line push back one after this line if you try this it increases its size and inserts one at the back yes even if you try this line after this it will increase its size and expand to a size of six so it is allowed its dynamic in nature you can always increase the size of vector even if you are predefining the size to be five remember this now how do you access elements in a vector one of the easiest ways is imagine your vector is like having twenty ten fifteen five and seven imagine uh this is what your vector as of now as the container as of now as and this is the zeroth index as i told you the first index second index third index and fourth index the best way uh to access them is you can say v1 so if you say similar to array if you say v1 it means n this actually means n if you say v3 it actually means 5 so you can actually access it in the similar fashion as you do for an array okay that's how you can access it that's one of the ways like you can write directly v0 or you can write v dot at generally uh people don't use this so you can just avoid it you can simply uh use the stuff that you use in array okay what's the other way the other way is an iterator yes the other way isn't now i was telling you that through uh through the lecture we will be learning about containers and hydrators so let's understand iterators what is the nitrato imagine the vector like let's take the same vector only 15 and 6 and 7 imagine this was the vector okay now if i write like for iterator you have to write this whatever whatever data type you have taken vector that data type double colons and you have to write iterator and this can be anything this can be i t this can be by t that this can be anything this is just a name but the syntax is the data structure data type double colon iterator this and you write v dot begin so i traitor basically means it points to the memory address because these guys would be stored somewhere in the memory like it this 20 would have been somewhere stored in the memory right and that memory can have any address the address can be 8 000 5 67 something something remember the address can be anything this 10 will be right after that this 15 will be right after that right so all these possible values are actually stored in memory so what we do is if we write v dot begin it actually points to that memory it points directly to the memory right not to the element it points to the memory understand that v dot begin means it's pointing to here but on the memory okay so if you're trying to print v dot begin you're printing the memory address not the element and in order to access like if you have read about c plus plus in order to access anything that is in the memory you just write star like if i write star v dot begin understand this v dot begin is going to give you the memory this portion and the moment you write star the element inside this is accessed now let's understand again if i'm writing v dot begin this is actually pointing to the memory where 20 is okay the next time i'm doing i'm saying hey i trader can you just move ahead so this begin does is this goes here so now the iterator instead of begin is right at the next memory address because i have shifted the memory and inside all of these are contiguous memory locations all of these are contagious memory locations so if you are shifting it plus plus it moves to the next memory again if you do it plus plus moves to the next memory after that if you're doing star of it what will happen what will happen as of now it was 20 so if you do start of id this actually prints 10 because it has shifted to 10 and now you're saying star access the value at that memory so you get 10 i hope that makes sense okay what if right after this i do an i t plus 2. so basically i'm saying shifted by two positions to this portion shifted directly by two positions to this portion which has a six now if i try to print this it will print six so this is how you can easily use the iterator i twitter is nothing but points to the memory where the element is lying i hope that makes sense okay so we are talking about iterators now you must be thinking do we have any other iterators apart from begin we do have we have something like end reverse end and reverse begin okay now imagine i have something like 10 20 30 40 as the vector okay so this is what the vector is so if i'm saying vector dot end like where does this point to remember this end will not point to this portion end will not point to this portion instead of that end will point to somewhere right after 40 the memory location that is after 40. now if you on this iterator do an i t minus minus then this iterator will move to 40 then only this will move to 40 so end points to a memory location that is right after the last element please understand this is very very important in terms of iterator so you understood about end but what about something like reverse and these couple of things are never used but just just know it like it's never ever used what is reverse and reverse end basically means i am going to reverse this i'm going to reverse this so apparently the reverse is 40 at first then 30 then 20 then 10 so now the end is 10 so right after end so that's this position is where it will be pointing as this position is where it will be pointing reverse and end means right after right and reverse begin will be pointing to this reverse begin will be pointing to this and there is a specific thing about this it moves in the reverse way now if i try to do it plus plus on this if i do i t plus plus i t as of now is pointing here i t plus plus will move here yes and after this id plus plus will move here so it's a reverse i trade i have to think it in the reverse way like if you just uh think this array in the reverse it's 40 vector rather 40 30 10 uh 2010 and now if i talk about end this is end and if i talk about begin this is begin and if i say begin plus plus it moves to 30 so it's that way reverse everything is in the reverse order never used do you need to know just know it for the sake of knowing but no one is going to ask you we have uh discussed about v of zero v of at of zero what is v dot back as the name recommends if the vector is having something like this v dot back means the element which is at 30 is the element which is at 30 that is the meaning of this now if i'm if i'm wanting to print the vector there are a couple of ways imagine i have 10 20 30 as the vector and i want to print it the simplest way is i know the indexes are 0 1 2 so i can directly loop from 0 1 2. like i can just go across and say key i'll loop from 0 1 2 and print it that's a very simple way the other thing is i say i'm going to do it i trader wise because i know this guy is begin so i did begin and i know the last guy is end right after the last guy is end so i'm going to run the iterator till it does not reaches the last guy i'm going to do an id plus plus the first time i get 10 next i move it i get 20 next i move it i get 30 and i will print every time star of id and this is how you can print the entire vector this is how you can print the entire vector but but but there is a shortcut now you must not be like no one wants to write these long syntaxes because stl means short like standard template library but it means everything in a very simpler fashion so still gives you something like auto so if you write auto it automatically assigns it to a vector iterator you don't have to say that this is a vector iterator you don't need to define the data type it automatically defines the data type now even if you write integer a equal to 5 so you're defining a to be of integer data type even if you write something like auto of a equal to five computer automatically says that this is an integer so this a will be of integer data type so if you write auto the data type is automatically assigned according to the data according to the data the data type is automatically assigned like if this would have been something like raj string and i would have written auto of a a would have been automatically string so auto means auto assignation so that's that's the beauty of c plus plus if for some time you don't know the data type you can just write auto c plus plus will take care of it and it will automatically assign the data type for you the other way to print the vector is using the for each loop so if i use this for each loop which is i t on v so it's basically means if the vector is 10 20 and 30 i'm saying i t first time id is here next time id is here next time id is here and you can simply print the id auto means on the data type please i trade on the data type first nitrates on the 10 not in not an iterator not not an iterator this means over here int because it is of integer data type automatically i trade 10 then 20 then 30 and automatically prints it entirely so that's about how to declare a vector how to use it in a vector how to print a vector now let's understand the deletion in a vector so imagine i want to delete something so there is something as an erase function and if i have the vector like 10 20 12 23 i say begin dot plus one now in order to use the erase function there are a couple of things either you give the iterator that click the location of the address the location of the address that you want to delete that this is the address that i want to delete please please delete this address so i'm saying okay begin plus 1 what does this mean begin plus 1 means 20 so if you just do erase of this address so the vector will be now 10 12 13 the vector will be reshuffled the vector will be reshuffled into 10 12 and 13 that is how the reshuffling will go on so that is one way to erase so we have understood how to erase single element but what if i have a vector like 10 20 30 and 40 and 50 and i says driver i want to delete these couple of elements i don't want to go single do you have something i say yes i have and that's like it is and i say give me the starting address and give me the end address after the element very important end address after the element so starting if i want to delete 20 can i say that's nothing but begin plus 1 i can say because right after begin that is where 20 is i want to delete 30 that is begin plus 2 but i said right after what you want to delete so that's begin plus three that's begin plus three three is pointing to here so i'm gonna delete this portion but you have to give this end after after the guy that you want to delete after right right after 30 you have to give the address so apparently it deletes start and this is something like this end is not included the start is included got it so please make sure you give something as which is not included right after one right after that okay so that's how you can easily delete it okay so for this example we have 10 20 12 23 35 what do you mean by plus 2 that means 12 what do you mean by plus 4 plus 2 plus 3 plus 4 so plus 4 is here so apparently this and this will get deleted and you will be left out with 10 20 and 35 i hope that makes sense now we're going to learn about insert function insert again if you want to insert something it's very simple first of all if you declare this this creates 100 like two instances of 100 in a container now if i want to insert a 300 right at the start right at the start so i say uh dot begin and please insert 300. so this does is this inserts right at the beginning okay 300. now imagine you had like 10 20 30 40 okay and i want to insert something here on inside of five here so do you write this is the first position this is the first position so instead of begin you have to write begin on the first position can you please insert five so if you write this this five will go here and get inserted right at the first position that's how you do insert now that was for single element you inserted single element what if i say that you have 10 20 or 30 40 as the vector i want to insert two fives imagine i want to insert two fives how do you do that so you say i wanna insert at the first position the number of elements that i to insert and the number that i want to insert so this will do is this will take 5 and 5 and insert it right after 10 so this makes it 10 5 5 20 30 40 so if i say over here you had 30 100 100 so i'm saying begin plus 1 which is right after 300 two positions like two two occurrences of ten two occurrences of ten inserted understood very simple so that's how the insert function does work for this now what if you have a vector and you want to insert it into some other vector now imagine i say that okay i have a vector like for this example we have 50 50. so i had a 50 and 50 vector okay now this vector is named as copy so this is the line that declares that okay now i already had this vector 30 10 10 100 100 and now i want to insert this 50 50 somewhere so add that somewhere i give that address and i say please and enter this entire vector please enter this entire vector so it will easily take this entire vector and enter it if you want to have a portion of this vector you can give that starting portion and you can give this after end portion and that will also do it again not required what is required is it is and this portion this is hardly required if you just know erase and insert about a single element does okay that's how you can easily do about vector now what are the other functions in vector v dot size will give you how many elements are there in the vector as of now dot pop back if this is the vector pops out the last element dot swap is very simple if this is a vector v1 this is a vector v2 it swaps the vector as the name recommends v dot clear doesn't matter how big your vector is trims it down to an empty vector trims it down to an empty vector erases everything okay and v dot empty says does your vector like if your vector also has like a minimum of one element it says not empty not empty but if the vector has nothing it will say true empty so these are the functions that are generically required in a vector so the next container that we will be learning is list a list is exactly similar to vector but the only stuff in list is it is it gives you front operations as well now list is a container again dynamic in nature same way of declaration you can push back to you can in place back forward so this is the list that will happen if you push back to n4 and after that if you say push front this front goes over here five like it directly pushes it into the front in vector you have to use the insert operation and if you're inserting somewhere that does take a lot of time like insert function in a vector is very costly like we will we will read about time complexities in further data algorithmic lectures but as of now just remember an insert in a vector is costlier and a list since the internal operation is a doubly linked list like a doubly linked list is maintained for a list and for a vector a singly link list is maintained so thereby something like push front is very very cheap in terms of complexity time complex device when you compare it to a vector okay and there is employees front as well all of the functions begin r and reverse and size clear empty all other functions are similar to vector so i will not be explaining that okay so that is about list now the next container that we will be talking about is dq again similar to list and vector you just declare it push back push front pop back pop front back front and all of the functions are similar not going to explain this as well it is exactly similar to less than vector so the next container that we will be learning is stack now stack is something as lifo lifo means last in remember this last in first out the guy who went in last is the guy who will come out at first so generally you can uh just imagine a stack to be a data structure like this and this is how you declare a stack stack the data type and the variable name okay so declare the variable name like this i'm saying uh push one so push one i'm saying push to i push to as i push three i push three i'm again seeing push three i push three i see employees is similar to push so i say push five now these are the push operations right after that if someone says stack dot top so as i said who's the last guy who went in the last guy that went in you know is nothing but five so this will print five is this will print five now realize this over here indexing access is not allowed you cannot say this is index zero this is index one something like this you cannot see in stack there are only three functions one is push one is pop the other one is top all other there like size uh clearer there but these are the generic three functions that you have to deal with so if i'm saying top it gives you five but the five is still in the stack the five is still in the stack now the moment i say pop it deletes this from the stack now five is not in the stack now if i'm saying top right before five was there three said prince three now if i'm at this moment saying stack dot size there are four elements so i print four i'm saying stack is the stack empty the answer is false it has four elements now if i'm saying swap it to some other stack i declare another stack one and i say can you please swap it so swap is very understandable both uh both the guys will swap so i hope push is clear pop is clear pop means delete top means just tell me what is that top you don't need to delete just tell me what is at the top so that is how the stack stl works like now talking about complexity wise in stack all the operations are we go off one operations everything happens in constant time so let's learn about the next container now now the next container that we will be learning is a queue data structure now a queue data structure is similar to stack but over here it is fifo okay fifo means first in the guy who gets in first comes out first stack was last in okay now you can uh you can just think it in this way like if you if you're forgetting names you can say like you go into a platform and if you're buying a ticket what do you generally do the person comes in the first person who comes and stands and the next person who comes in and stands so the first guy who gets the ticket this guy then this guy gets the ticket and if someone is coming it's a queue that that happens so that that is where the concept comes in first in first out so if i'm saying push one i push one push two i push to push four so i pushed one two and four and the next line i'm saying q dot back plus equal to but back will mean four it does not means this guy back will mean four only so over here i'm saying add plus five this makes it nine i'm not saying q dot back so if i'm saying q dot back it prints the last guy nine okay i'm saying q dot front prints one just prints does not deletes if i'm saying q dot pop deletes deletes deletes the front guy first 10 guy q at front is now 2 so it prints 2 it is similar and now all the operations are much more similar to stack size and all these things that's how q works again all the operations are happening in constant time the next thing that we will learn is priority queue now as the name recommends priority the guy who has the largest value stays at the top it's similar to queue but there is some something that happens inside which you'll learn probably after a couple of years when you are appearing for interviews but as of now just understand the logic okay so remember if you're declaring priority queue like this the maximum element stays at the top or the largest element if you're using character the largest character if you're using integer the largest integer if you're using the string the lexicographically largest string will stay at the top okay so i'm saying q dot push five so you push in five i say q dot push two so you push in two i say q dot push eight so you push it eight now i say q dot push n say position ten okay now the moment i say pq dot co out of all these elements which one is the largest 10 so it prints 10 and that is the guy who will be at the top now if you are trying to insert something like 3 3 the 3 will go right here the 3 will go right here and this is not a linear data structure inside of it a tree data structure is maintained which you learn in the later half now understand one thing the data is not stored in a linear fashion in like inside at inside a tree is maintained okay which probably you learn someday okay so as of now if i say pq dot pop the topmost element is popped n is popped if i say again or pq top is the topmost 8 yes so this is how the priority key works again push top and pop main functions the other ones are size and md size and md is very simple and swap is also very simple so these are the functions what if i want a priority queue which stores the minimum element at the top then this is how the syntax is priority queue data type vector and this greater end you have to write and if you give this data type and now if you push 5 5 is there if you push 2 towards then if you push 8 8 is there if you push 10 10 is there but this time if you try to access pq top 2 will come out so that's how you maintain a very simple minimum priority queue and generally it's known as mean heap and this is known as max heap remember this the terms that you learn in ds algo as you move forward what is the time complexity of push push happens in logarithmic of n top happens in b go of one and the pop which is the deletion again happens in logarithmic of n so this is how it happens if you don't know what is logarithmic of n no issues just keep this in your mind as you move forward you will understand india's algo now the next container is very very interesting and that is nothing but the set container now what is set just remember one thing it stores everything in the sorted order and stores unique just remember these couple of points and you know what is a set everything in the sorted and just unique just two points and you're done let's understand so imagine this is the container okay and i say insert one so you insert one imagine you say in place two so you insert two imagine you say insert two unique so will it store two again no it does not no it does not it does not stores too if i say insert four will it yes if i say insert three it will but it will insert it here again a very important thing sorted stores in a sorted order at first it will have one then it will have two then it will have three then it will have four everything in the sorted fashion so it stores everything in the sorted fashion so this is how the set will be storing again a container is it a linear container no a tree is maintained so i'm just explaining you this via this bucket but inside of this there is an entire tree which is maintained again which you learn as you move forward so insert and place works in a similar fashion sorted and unique okay now there are functions if i say set dot find three and this is the set so it will return an iterator remember this returns an iterator which points to this three which points to this three it returns an iterator which points to this three okay so basically this is an iterator remember iterator points to the address perfect now if i say set dot find six is six here no if an element is not in the set please hear me out properly if an element is not here in the set it will always return set dot end that means an iterator which points to right after the end imagine the set is having one two four five and you did set dot find three okay and you don't have a three so you will have an iterator end this is the iterator which points after five after five that's where dot find will turn the iterator to be it points afterwards okay and after this there is set dot it is very simple erases this guy five it raises this guy five out of it like if this is the set if this is a set it will delete five it'll simply delete five you don't have to think anything deletes five and maintains the sorted order deletes five and maintains the sorted order now as i said set is nothing but unique and sorted so if you're trying to count if it exists if it exists it will only have one occurrence because it does contain unique and if it does not exist it will have zero so if one is there in the set it will give you one like one occurrence if it is not it will give you zero as simple as that okay you can also erase like you can either give the element to be erased like you can give the element to be erased or you can give that okay this is the address or the iterator please go and erase this iterator as simple as that now in vector we did learn about it is start comma end similar thing also works over here if you want to erase everything yes if you want to erase everything between two and four imagine you had something like this so two is here four is here if you get the first guy if you get the second guy so if you do find you'll get the two zai traitor if you do find a four you'll get the force iterator so it deletes two and three remember this it deletes two and three not four four is this bracket and this is this please remember this now in set there are other functions like size empty swap everything is similar to vector that is begin all of these are similar to vector so i will not be explaining them the most important are find count and insert these are the most important ones and as well as it is now they have something as a lower bound and an upper bound so i will be linking a video in the description which explains lower bound in depth and upper bound in depth so please go back and watch lower bound and upper bound once you have seen that you will actually understand how does this low bound and upper bound work in a set it's the exact same that will be taught in the video which is in the description so please make sure you watch it now in set everything happens in logarithmic time complexity if you're inserting it takes logarithmic if you're erasing it takes logarithmic everything happens in a logarithmic time complexity again if you don't know log in no issues please remember this in your head now we did learn about set and i said sorted and unique that means it will just contain one occurrence of two you can insert thousands of occurrences of two but it will just store one occurrence of two but there is something as multiset if you define multi set it only obeys sorted and will not over unique it will store multiple occurrence like if you try to insert one one one stores all the occurrence and you try to erase one but all the occurrences are erased this time but if you do an it is one it erases every one and this time count will count you the number of ones in the multi set but if you want to delete imagine your multiset is containing three ones and i just want to delete one occurrence of one or two occurrence of one or three occurrence of one so what i can do is i can just find out the first occurrence of one so i'll just do multi set dot find because i know find points to the iterator and also erase that iterator instead of saying it is element because if i say understand if i say erase element it erases all the elements but if i say it is address it only erases that portion it only raises that portion and i don't know it is like two two ones so i say it find one and go till two go till two so it really raise both of them both of them so either it is element it is address or it is starting address and right after the end that's it diet raters and rest all functions are same as set stores everything in the sorted but not unique stores multiple occurrence like one one one two three three four it can store multiple occurrences as well so that is the definition of multi-set now we did learn about set we did learn about multi-set now there is something as unordered set everything is similar to set the only thing that is omitted like it stores unique the only thing on it is it does not stores in the sorted order we don't know how it will store it has randomized order it has randomized order like if i put in one after that i put in five after that i put in two after that i put in three after that i put in six it can have this order it can have any order in the world but it will just have unique elements like if i try to insert one again it will say i have one i have one and in most of the cases the time complexity is b go of one everything like all the operations are same insert erase all the operations are same but only the lower bound and upper bound function does not work all the operations work but the lower bound and the upper bound functions do not work remember this all operations are similar to set and they do not store everything in sorted order so all the operations are generically in a big o of one constant time but in the worst case which happens once in a millennium like if the data is through like possibly given in such a way that they want you to explore the worst case which does not happen then the unordered set goes for a we go off and linear time it goes for the worst case again does not happens every day happens once in a blue moon the time complexity goes till we go off and you just write them down in notes you will understand these things when you move across or when you grow in experience that's how the unordered site works so the next container that we will be learning is a map container okay now you can think this as something like just take a task when i say in your college there will be like there might be multiple people with raj name but how do you distinguish themselves by roll numbers right one raj might have a roll number of 23 the other raj might have a roll number of 25 the other raj might have of 28 so you know this one guy is of roll number 23 the other guy is the roll number 25 the other guy is of roll number 28. so generally this is uh in in your class this is store like this roll number one roll number two roll number three and so on if there are 50 people you store it like this so map you can think this as a as a data structure or a container which says the roll number is my key and over here the value can be the name so this is what the data structure means you store unique keys because you can't have 23 rule number twice you can have it just once so the keys are unique the keys are unique but the values can be like over here there can be a raj over here there can be a rod so there is one guy with key three was raj there is one guy with key 50 who is raj there can be duplicate values but it has to be a unique key so you can think map as a container which stores everything in respect of key and values and very important thing this key can be of any data structure it can be integer sorry any data type it can be integer it can be double it can be pair it can be anything similarly this value can be anything so how do you define map this is key this is value he is integer value is integer over here he is an integer and they're saying value is two integers over here they're saying key is two integers and value is one integer so you can define it as you wish to that is on you this is how you define okay now if i'm defining the map to be this for an example assume you're defining the map to be this now i say one equal to two it means on the key one can you please store two so this is what it stores internally in the map internally in the map it stores one comma two next i say i don't want to store it like this and place three one so it stores three is the key and the value to three is one it does a similar thing stores it into the map again i say insert you can also use insert two comma four goes and stores two comma four and this is how you can store all these three variables okay so in this way you can actually store for this particular declaration and remember one thing map stores unique keys very very important map stores unique keys in sorted order something similar to set data structure map stores unique keys in sorted order something similar to set data structure okay now this is the declaration the second declaration for this declaration sorry or this declaration this is the key so you have declared the key like this and the value is a single integer so this is it for this it will be storing like okay key is 2 comma 3 so this is stored and the value corresponding value is 10 perfect so this can be stored like this that's how it generically stores okay i hope that makes a lot of sense i told you that everything is stored in a sorted order so this will at first store 1 comma 2 then it will store 2 comma 4 then it will store 3 comma 1 this three lines will be storing like this so again if you want to explore or if you want to iterate on the map one of the ways is you start from beginner nitrato and you go until end iterator similar in the vector all you do is you say i t you run a for each loop first time id is here so it stores in a pair next time id is here stores in a pair next time id is here it stores in a pair if it is storing in a pair this is id dot first and this is id.second so first time prints one two next time goes here prints to four next time goes here prints three one so we try to do this c out this is how you can actually traverse in a map and everything is stored in a sorted order of key in a sorted order of key not value sorted order of key remember this sorted key is how it stores no duplicates all uniques okay now if i want to access map of one if i want to access map of one it says a value two it says a value two because at one you're storing to but if it tries to access five will it find five there is no five so what happens it it says null but if you want to print it it actually goes on prints zero or null okay because it does not exist so if something does not exist it gives you zero okay so this is how you can easily access for a key now if you wanna know the iterator imagine you want to know where the key2 lies the address of it so again the find function will come over here and say map.find3 so this is where you get it diet rater in order to access this you give a star so this access is this and we want the value dot second got it it gives you the iterator to the this three comma one okay this id is this so if you give a star it's the element and if you give a second it has the one that's how you can easily access this as well now over here if you try to do dot find five and five is not there it points to nothing but dot and and means after the map after the map okay make sense and again the lower bound and upper bound functions if you have seen the video in the description you can understand how lower bound and upper bound works okay and all the other functions like it is swap size empty are same so i'm not going to explain it again next thing is multi-map similar to map only thing is you can store duplicate keys you can store duplicate keys something similar to set and multi set as i told you right duplicate keys but everything in the sorted order this time you can store like one comma two and then you can come across and again store like one comma three right so you can store duplicate keys over here unordered map again unordered map is similar only this portion will go across it will not store in sorted it will be randomized it will be randomized but it will it will not have duplicate as well it will just have unique keys unordered map will have unique keys but it will not be sorted and the difference is like the map works in logarithmic of time and the unordered map in the in almost all cases works in constant time in the worst case it goes for big offen again this worst case happens once in a blue moon not always in almost all the cases we go of one is what appears so as of now i can say that i have completed containers and iterators now this is not required not required no need to learn it just omit this now i'll be telling you the all the important algorithms like which are mandatory like you should know and all the other algorithms which i will not teach you will eventually learn while you code but that are not like those are not important so as of now you can just leave it eventually with time if it's required learn at that moment no need to learn it now so let's move across to learn some algorithms now if i say you that hey listen i give you an array of size like one five three or two okay of size four and i want you to sort it so you will be like let's apply bubble shot my shot selection sort so and so but in c plus stl if you just write the line sort a comma it's a four size so this actually means the first position the first position of the first nitrator the starting nitrator and this means the last titrator a plus four actually means this portion then portion the last iterator again similar to something like start start is included and end is not included so you write the starting iterator which is a which actually points to this and a plus 4 which actually points to this so all the elements are sorted right after this line it will have one two three five so you don't have to actually use moist short bubble short selection sort it sorts that into one line and if you're using vector the starting is begin this is the ending the starting iterator and the ending iterator so in this way you can sort any container not map all like all not map i'm talking about vectors and arrays over here okay now what if i just wanted like i had something like one three two five and rather let's keep it like five two okay and i wanted just this portion to be sorted so i know a plus two because this is a plus two for sure i know a plus 4 right after this is a plus 4 so only this portion will be sorted so this is how only this portion will be sorted perfect what if i want to sort them in descending order imagine you have like 1 3 5 and 2 i want to sort them in descending order so it's very simple give the starting iterator give the ending iterator the portion that you want to sort and just write greater end yes just right greater in and this is nothing but a comparator an inbuilt comparator which automatically sorts it like i'll teach you comparator automatically will sort it in the descending order like this in a descending order you just need to write greater end and it automatically sorts it into a descending order now what if i want to sort it in some other fashion because as of now we know how to sort it in increasing we know how to sort it in decreasing but what if i want to sort it in my my way because going across you will see that this my way is being used a lot for an example we have declared a pair array and these are the pairs one two two one four one okay and i want you to sort according to second element like i want you to sort it in in order of increasing second element please understand i want to sort it according to increasing second element okay and if the second element is same then sort according to the first element but in decreasing but in decreasing what do i mean by that so as of now can i say the second element is 2 1 and 1 so this 2 comma 1 and 4 comma 1 are the guys who should appear right at the first okay because the second elements are one and one and after that i can say after that one comma two will appear because because of this portion so can i say that i've sorted according to the second element but i still have a problem now these couple of guys are having same second element now if they have the same second element i want you to sort it according to the first element but in descending which means i want you to have 4 comma 1 at first and then 2 comma 1 that means among them among them i wanted to sort it according to descending so first 4 then 2 and then you can write so first sorted according to the second and if there is a group which is having the same second then among them sorted according to the first so this is my way this is my way okay it's it's something like a combination of increasing as well as decreasing so this is where generically you write the first iterator the last iterator and a comp and a comp perimeters and a comp now this is nothing but a self-written comparator a self-written comparator and this comparator is nothing but a boolean function is nothing but a boolean function so i've written this but i'll just teach you how to write it it's very simple you write boolean comparator and this is the function okay this has to return a true and a false this has to return our true end of false now go back and see what is the data type that you that you just had and the data type if you see was pair of ended just copy paste and have couple of guys pair one and pair two that's it have couple of guys this is the first thing that you'll do have couple of guys now please understand this that this is player one and this is pair two so forget about the array forget about the array and now think of this two instances where you have two pairs where you have two pairs this is p1 this is p2 while writing comparator just focus what have you been said sorted according to the second element so you say okay okay if my p1 dot second is already smaller than second that means it's true we are assuming we are assuming that the pair one lies before p2 lies before p2 that is what the assumption is the pair one lies before p2 and that is okay if the second guy is lesser than the second guy i'm okay they should actually so they are in the correct order this competitor says are two guys in the correct order or not and i'm saying they are in the correct order they are in the correct order because this guy is smaller than this guy and that is what i was said so if they are they are in the correct order but but i know one thing if it's the opposite if this is the case i will say they are not in the correct order because if i have two guys okay and imagine this is five and this is four and i'm saying p1 occurs before p2 this is wrong this is wrong if this second is actually greater than this i know this is wrong so i say they are not in the correct order so what happens is comparator internally says p1 and p2 are not in the correct order can you please swap them so apparently four comes before five this is what happens so they do internal comparisons and they swap so i told them false they are not so they will swap internally but do we have any other conditions we have what if they are same because that's the only condition that is left that is the only condition because if if these two conditions do not happen i know we will come to a point that they are same i don't need to write if because that's the only condition that is left they are same now if they are same will again try to evaluate i know if they are same this is p 1 and this is p 2 and this time it is descending so this guy is greater than this guy it's okay otherwise it's not so can i say if p1 dot first is actually greater than p2 dot first it's okay because this is what i was looking for else i say it's not if they're equal it's fine i i even if i swap or do not stop it's okay so can i say if it's greater it's okay that's what i'm looking for whatever it's not that's false please swap it if it's not i need in descending can you please just swap it so you just analyze everything whenever you try to write a comparator always analyze everything in terms of two pairs don't think in terms of arrays just pick up yes i repeat you just need to do one thing just pick up one pair just take another pair and try to analyze p1 and p2 that is your job so you have learned about comparators so anytime there is my way my way sorting you can write the comparators just need to write this and you should be done just focus on the data type and try to just have evaluate two data types and write it nothing different okay so this is again one more stl which is very important which is built in popcorn so if number seven what is the binary of seven it's one one one that's the binary of seven so there's a built in pump count it says okay this has three bits as one typically uh number seven means zero zero zero zero zero zero like these are the 32 bits inside the computer in 32 bits it's zero zero zero zero zero zero and one one one so built in popcorn says how many ones are there or how many set bits are there so it will return three set bits if this number would have been six which is nothing but one one zero six is nothing but one one zero so built in popcorn would have returned two the number of set bits okay if the number is long long then built in popcorn becomes built in popcorn ll built in popcorn ll if the number is long long integer will not suffice that perfect now the last thing is not the last second last thing is next permutation now if i write a string as one two three and i want to have all the permutations of it all the permutations so what i can say is okay listen i'm gonna have the string and i know i know the next permutation is one three two i know the next permutation like if i talk about dictionary order 2 1 3 the next permutation is 2 3 1 the next is 3 1 2 the next is 3 to 1 can i say these are the dictionary like these are the six permutations that you can have three factorial is six so if you want to print them what you do is you first print the first string then you say can you have the next permutation please and you have the next permutation and it takes you the string which was this becomes 132 and now you print it again the string now becomes 2 1 3 and you print it again the string becomes 2 3 1 you print it again the string becomes 3 1 2 and you print it again the string becomes three to one and you printed right after this it goes to null it says no more permutations it returns a false if there are no more permutation it returns a false and if it returns or false the while loop breaks and this is how you can print all permutations of a string here's a catch what if the string was 231 then it would have started from 231 an x permutation of 231 is 312. and the next is 321. so it would have just printed like first tense then this then this right after this no permutations so it's very important that if you want to print all the permutations you start from the sorted guy like you just start from the sorted guy and you can easily sort it you know the stl now in this fashion you start from the sorted guy and that's how you can easily print it now the last one is max element imagine you have an array like 1 n 5 6 and even the maximum element so if you give max element star titrator and iterator it gives you the address and if you give the star it gives you the element similarly main element is also there right so these are the algorithms that are generically used in ds algo in your day-to-day life all the other algorithms are there but they are not widely used and you will not be requiring them so whatever stl i've taught in this video is more than enough to get started with c plus so just in case you've understood everything it's an honest request that get into the comment section and just write one line comment because that is the only thing that keeps you motivated to make these kind of content and if you're probably the first time on this channel please do consider subscribing because i have a lot of content regarding trees graphs dynamic programming dsa that might help you in your longer run so please make sure you subscribe to this channel as well and you can like this video and yeah please please do comment because that keeps me going with this i'll be wrapping up this video let's meet in some other video till then bye take care [Music] don't ever forget